简体   繁体   中英

Swing table view in netbeans

I am using Swings in Netbeans. I have created button named "view" when I click view button it should fetch database table and display the table below view button on same page.

My code:

private void viewActionPerformed(java.awt.event.ActionEvent evt) {                                     
   showData();    }                                    


public void  showData()
    {
      Connection con=null;
        Statement st=null;
        ResultSet rs=null;
        String s;
        try
        { 
            con=DriverManager.getConnection("jdbc:mysql://localhost/testm","root","root");
            st=con.createStatement();
            s="select  * from smpl";
            rs=st.executeQuery(s);


        ResultSetMetaData rsmt= rs.getMetaData();
        int c=rsmt.getColumnCount();
        Vector column=new Vector(c);
        for(int i=1;i<=c;i++)
        {

        column.add(rsmt.getColumnName(i));
        }
        Vector data= new Vector();
        Vector row= new Vector();
        while(rs.next())
        {
         row=new Vector(c);
         for(int i=1;i<=c;i++)
         {
             row.add(rs.getString(i));
         }
         data.add(row);
        }

    JFrame smp=new JFrame();
    smp.setSize(1000,500);
   // smp.setLocation(0, 300);
     //smp.setUndecorated(true);
       smp.setLocationRelativeTo(null);
         smp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel=new JPanel();
       //  paneldes.setLocation(20, 100);
         JTable table= new JTable(data,column);
         JScrollPane jsp= new JScrollPane(table);

         panel.setLayout(new BorderLayout());
         //paneldes.setLocation(100, 200);
      panel.add(jsp,BorderLayout.CENTER);    
       // paneldes.add(jsp); 
       // jsp.setLocation(100, 200);
        this.setContentPane(panel);
         this.setVisible(true);
        SwingUtilities.getAncestorOfClass(null, jsp);
    }

        catch(Exception e)
                {JOptionPane.showMessageDialog(null,"ERROR");
                }finally{
                        try{
                        st.close();
                        rs.close();
                        con.close();
                        }catch(Exception e){
                        JOptionPane.showMessageDialog(null,"ERROR CLOSE");

                }}}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new smplvi().setVisible(true);
            }
        });
    }

thanks in advance

Try doing it like this, not with a vector

     DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
     String data1 = something1.columnOneFromDB;
     String data2 = something2.columnTwoFromDB;
     String data3 = something3.columnThreeFromDB();
     String data4 = something4.columnFourFromDB();

     Object[] row = { data1, data2, data3, data4 };

     model.addRow(row);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM