Most simple code to populate JTable from ResultSet

前端 未结 10 848
慢半拍i
慢半拍i 2020-11-22 11:22

I googled the whole day and no luck. I call getnPrintAllData() method after pressing OK button. So the code is:

public class DatabaseSQLiteConne         


        
相关标签:
10条回答
  • 2020-11-22 12:04

    I think this is the Easiest way to populate/model a table with ResultSet.. Download and include rs2xml.jar Get rs2xml.jar in your libraries..

    import net.proteanit.sql.DbUtils;
    
        try
        {
        CreateConnection();
        PreparedStatement st =conn.prepareStatement("Select * from ABC;");
        ResultSet rs = st.executeQuery();
        tblToBeFilled.setModel(DbUtils.resultSetToTableModel(rs));
        conn.close();
        }
        catch(Exception ex)
        {
        JOptionPane.showMessageDialog(null, ex.toString());
        }
    
    0 讨论(0)
  • 2020-11-22 12:08

    This is my approach:

    String[] columnNames = {"id", "Nome", "Sobrenome","Email"};
    
    List<Student> students = _repo.getAll();
    
    Object[][] data = new Object[students.size()][4];
    
    int index = 0;
    for(Student s : students) {
        data[index][0] = s.getId();
        data[index][1] = s.getFirstName();
        data[index][2] = s.getLastName();
        data[index][3] = s.getEmail();
        index++;
    }
    
    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    
    table = new JTable(model);
    
    0 讨论(0)
  • 2020-11-22 12:08

    You have to Download the Jar called rs2xml by click this link rs2xml.jar Then Add it to your project libraries then import

    import net.proteanit.sql.DbUtils;
    

    I prepare Only a method to do so as a sample

    private void Update_table(){
        try{
            // fetch a connection
            connection = your_connection_class_name_here.getInstance().getConnection();
            if (connection != null) {
            String sql="select name,description  from sell_mode";
            pst=connection.prepareStatement(sql);
            rs=pst.executeQuery();
             your_table_name_for_populating_your_Data.setModel(DbUtils.resultSetToTableModel(rs));
            }
        }
        catch(IOException | SQLException | PropertyVetoException e){
        JOptionPane.showMessageDialog(null,e);
        }
        finally {
              if (rs!= null) try { rs.close(); } catch (SQLException e) {}
              if (pst != null) try { pst.close(); } catch (SQLException e) {}
              if (connection != null) try { connection.close(); } catch (SQLException e) {}
          }
    }
    

    Hope this will help more

    0 讨论(0)
  • 2020-11-22 12:13

    The JTable constructor accepts two arguments 2dimension Object Array for the data, and String Array for the column names.

    eg:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    public class Test6 extends JFrame {
    
        public Test6(){     
            this.setSize(300,300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Mpanel m = new Mpanel();
            this.add(m,BorderLayout.CENTER);        
        }
    
    
        class Mpanel extends JPanel {       
    
            JTable mTable;
            private Object[][] cells = {{"Vivek",10.00},{"Vishal",20.00}};
            private String[] columnNames = { "Planet", "Radius" };
            JScrollPane mScroll;
    
            public Mpanel(){
                this.setSize(150,150);
                this.setComponent();
            }
    
            public void setComponent(){
                mTable = new JTable(cells,columnNames);
                mTable.setAutoCreateRowSorter(true);
                mScroll = new JScrollPane(mTable);
    
                this.add(mScroll);
            }
        }
    
        public static void main(String[] args){     
            new Test6().setVisible(true);
        }
    }
    
    0 讨论(0)
提交回复
热议问题