Page 44 - 3-2
P. 44

table.setModel(new javax.swing.table.DefaultTableModel(


                                 new Object [][]
                                         {


                                         },
                                 new String []
                                         {
                                             "code", "name", "num", "price"
                                         }


                             ));
                    DefaultTableModel model = (DefaultTableModel) table.getModel();
                    table.setRowHeight(contentPane.getHeight()/14);
                    System.out.println("create");
                    scrollPane.setViewportView(table);
                    add(scrollPane);
                            contentPane.add(scrollPane,"Center");
                            contentPane.setBackground(new Color(0,255,0));


            java.sql.Statement stmt = null;   //  데이터베이스 연결 상품목록표시,
                            Connection con = null;
                    try {
                            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql://localhost/mdp?useSSL=false", "root", "raspberry");


                            stmt = con.createStatement();
                            ResultSet rs = stmt.executeQuery("SELECT * FROM mdp_table1;");
                            while (rs.next()) {
                                    model.addRow(newObject[]{rs.getString("code"),
            rs.getString("name"), rs.getString("num"), rs.getString("price")});
                                    }
                                    rs.close();
                                    stmt.close();
                                    con.close();
                            } catch (ClassNotFoundException e) {
                                    e.printStackTrace();
                                    System.out.println("DB Driver Error!");
                            } catch (SQLException se) {
                                    se.printStackTrace();
                                    System.out.println("DB Connection Error!");
                            } finally{
                                            try{
                                                            if(stmt!=null)


                                                         - 44 -
   39   40   41   42   43   44   45   46   47   48   49