2011年8月11日 星期四

[java] (16) List all data in jTable from database, Edit Cell and store it


class MyTableModel extends AbstractTableModel

  {



//     import java.util.*;  

       private ArrayList<String> colname;

       private ArrayList<ArrayList> data;



       public MyTableModel(ResultSet rs)

       {

          try{



             // 取得欄位數量

             ResultSetMetaData rm = rs.getMetaData();

             int cnum = rm.getColumnCount();

             colname = new ArrayList<String>(cnum);



             // 取得欄位名稱

             for(int i=2; i<=cnum; i++){

                colname.add(rm.getColumnName(i));

             }



             // 取得列

             data = new ArrayList<ArrayList>();

        

             while(rs.next()){

               

                ArrayList<String> rowdata = new ArrayList<String>();

                for(int i=2; i<=cnum; i++){

                   System.out.println("happy1  int i=2; i<=cnum; i++ ");

                   rowdata.add(rs.getObject(i).toString());

                }

           

                data.add(rowdata);

                System.out.println("afdafads");

                System.out.println(data);

             }            

           }

           catch(Exception e){

              e.printStackTrace();

          }

          

        

         

       }

       

           /*

     * Don't need to implement this method unless your table's

     * editable.

     */

     



      // This one can edit the cell

       public boolean isCellEditable(int row, int col) {

        //Note that the data/cell address is constant,

        //no matter where the cell appears onscreen.

            return true;

      

       }




    public void setValueAt(Object value, int row, int col) {

       

            System.out.println(value.getClass());

            System.out.println("setValueAt Row is :" + row);

            System.out.println("setValueAt col is :" + col);

            System.out.println("setValueAt :" + value);

            



            // After edit cell can store the data into table

            ArrayList rowdata = (ArrayList)data.get(row);

            System.out.println((ArrayList)data.get(row));

            rowdata.set(col, value);

            System.out.println(rowdata.get(col));




        }

        




       public int getRowCount()

       {



           

          System.out.println("getRowCount() data.size :"+data.size());

                       

          return data.size();

       }

       public int getColumnCount()

       {

          System.out.println("getColumnCount() colname.size :"+colname.size());

          return colname.size();

       }



       public Object getValueAt(int row, int column)

       {

          System.out.println("getValueAt");

          ArrayList rowdata = (ArrayList)data.get(row);

          

          

          System.out.println("rowdata :" + rowdata);

          return rowdata.get(column);

       }

       public String getColumnName(int column)

       {

          System.out.println("getColumnName");

          return (String) colname.get(column);



       }

   }

    

    private void jButton1ActionPerformed(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 qry = "SELECT * FROM total_calc";

//            String qry = "SELECT * FROM total_calc";

              String qry = "SELECT * FROM tabaco_product order by Tabaco_order " ;

            ResultSet rs = st.executeQuery(qry);

            

                        if (rs.last()){

                   int i = rs.getRow();

                    System.out.println(i);

                    

                    

               System.out.println(rs.last());    

               rs.first();

               

            }

          

 









 



            // put My TableModel(rs) into setModel

            data_jTable1.setModel(new MyTableModel(rs));





            

            rs.close();        

            st.close();

            conn.close();

            

        } catch (SQLException ex) {

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

        }

        

        

        

    }                              


0 意見:

張貼留言