2011年8月6日 星期六

[java] (12) select jCombBox and put selected item into database


    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        // TODO add your handling code here:

        

        Connection conn = null;

        PreparedStatement pstm = null;

        

        String url = "jdbc:mysql://localhost:3306/sale?useUnicode=true&characterEncoding=big5";

        String user = "testuser";

        String password = "test623";

        

         Calendar date = Calendar.getInstance();

         SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-dd");





        

        try{

           conn = DriverManager.getConnection(url, user, password);

           Statement st = conn.createStatement();

            

           

           for (int i=0 ;i<jTable1.getRowCount();i++){

//               for (int j=0 ; j<jTable1.getColumnCount();j++){

                    try {

                        

                    String DataZ = jComboBox1.getSelectedItem().toString();    //讀取 jComboBox1所選擇的項目,然後放入資料庫中

                    String DataA = jTable1.getValueAt(i,0).toString();

                    String DataB = jTable1.getValueAt(i,1).toString();

                    String DataC = jTable1.getValueAt(i,2).toString();

                    String DataD = jTable1.getValueAt(i,3).toString();

                    String DataE = jTable1.getValueAt(i,4).toString();

                    String DataF = jTable1.getValueAt(i,5).toString();

                    String DataG = jTable1.getValueAt(i,6).toString();

                    String DataH = jTable1.getValueAt(i,7).toString();

                    String DataI = jTable1.getValueAt(i,8).toString();

                    

//                     System.out.println(Data);

                     pstm = conn.prepareStatement("insert into total_calc "

                             + "(Shift,Goods_name,Unit_price,Stocks,Good_in_stock,Ship,Balance,Selling_goods,Subtotal,Space_reserve,Date) "

                             + "values  (?,?,?,?,?,?,?,?,?,?,? )" );

                     

                     pstm.setString(1,DataZ);

                     pstm.setString(2,DataA);

                     pstm.setString(3,DataB);

                     pstm.setString(4,DataC);

                     pstm.setString(5,DataD);

                     pstm.setString(6,DataE);

                     pstm.setString(7,DataF);

                     pstm.setString(8,DataG);

                     pstm.setString(9,DataH);

                     pstm.setString(10,DataI);

                     pstm.setString(11, dateformatter.format(date.getTime()) );

                     

                     pstm.executeUpdate();

                     



                    }catch(NullPointerException e){

//                      jTable1.setValueAt(0, i, j);

//                      JOptionPane.showMessageDialog(null,("第"+ (i+1) + "行 \n 第"+ (j+1)+ "行 \n 沒有輸入資料" ) );

                    }

//             }

 

           }

           



             

            pstm.close();           

            st.close();

            conn.close();

        }catch (SQLException ex) {

         System.out.println(ex.getMessage());

        }

        

        

    }   



Reference:


0 意見:

張貼留言