2011年8月6日 星期六

[java] (11) put data into database from jTable


    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";

        



        //Sql 所吃的時間格式 String

         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++){



                    try {



                  // 抓出資料

                    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();

                    

                   // 所要放入的格式

                     pstm = conn.prepareStatement("insert into total_calc "

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

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





                     // 資料一個一個放入

                     pstm.setString(1,DataA);

                     pstm.setString(2,DataB);

                     pstm.setString(3,DataC);

                     pstm.setString(4,DataD);

                     pstm.setString(5,DataE);

                     pstm.setString(6,DataF);

                     pstm.setString(7,DataG);

                     pstm.setString(8,DataH);

                     pstm.setString(9,DataI);

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

                     

                     pstm.executeUpdate(); //存完之後,馬上加到sql裡面

                     



                    }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 意見:

張貼留言