2011年8月6日 星期六

[java] (13) get data from database and put data into jTable


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

        // TODO add your handling code here:

        Connection conn = null;



        

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

        String user = "testuser";

        String password = "test623";

        





        

        try{

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

           Statement st = conn.createStatement();

           

           

           String Data_Shift = jComboBox1.getSelectedItem().toString();

           String Data_Year  = Year_jComboBox2.getSelectedItem().toString();

           String Data_Month = Month_jComboBox3.getSelectedItem().toString();

           String Data_Day   = Day_jComboBox4.getSelectedItem().toString();

           



//    ResultSet rs = st.executeQuery( "SELECT Balance FROM total_calc where Shift= '早班' and Date= '2011_08_07' "); 要注意裡面有有點 ‘ ’







// ResultSet rs     rs前面要有ResultSet 整行才算完整

           ResultSet rs = st.executeQuery( "SELECT Balance FROM total_calc where Shift ='"+ Data_Shift  +"' and Date ='"+ Data_Year   +"-"+ Data_Month  +"-"+ Data_Day +"'" );

           

            



           

         //  SELECT * FROM [table name] WHERE name = "Bob" AND phone_number = '3444444';

           

           int li_row = 0;

           while (rs.next()) {



// 因為rs.getString(1)  1是因為只有抓 Balance  資料,所以放在第一列裡面

//如果是 select * from table  因為抓好幾列資料,所以要指定那一列要顯示






                jTable1.setValueAt(rs.getString(1),li_row,3);





                li_row ++;

           } // while

        

           

           

           

            rs.close();             

            st.close();

            conn.close();

        }catch (SQLException ex) {

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

        }

    }



 



Reference:


0 意見:

張貼留言