简体   繁体   中英

Data fetched from sqliite database returns multiple instance of single row to JTable

I try to populate JTable with data fetched from the Sqlite database table through this code..

class UserRecords extends JPanel {

private static final long serialVersionUID = 6596285522176538665L;

String record_holderName=null;
public UserRecords(){} 

public UserRecords(String name) {
    this.record_holderName=name;

    Connection con = null;
    PreparedStatement st=null;
     ResultSet rs=null;
     JTable myTable = new JTable();


    try {
        con = ConnectDb.getDbConnection();
        st=con.prepareStatement("select email,password,servicename,accounttype,hint,lastupdtime from usergems where record_holder=?");
        st.setString(1,record_holderName);
        rs = st.executeQuery();
        ResultSetMetaData md= rs.getMetaData();
        int columns=md.getColumnCount(); //holds number of column in resultSet

         DefaultTableModel model= new DefaultTableModel(); //this object pass data into JTables

        Vector<Object>columns_name=new Vector<Object>();
        Vector<Object>data_rows=new Vector<Object>();

        for(int i=1;i<columns;i++)
        {
            columns_name.addElement(md.getColumnName(i));
        }
        model.setColumnIdentifiers(columns_name);

        while(rs.next())
        {
            for(int j=1;j<columns;j++)
            {
                data_rows.addElement(rs.getString(j));
            }
            model.addRow(data_rows);
        }

        myTable.setModel(model);

          } catch(Exception e){
        String msg = "ClassNotFoundException: Not able to load the db drivers\n Details: \n" + e;
        JOptionPane.showMessageDialog(this, msg, "Error!", JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    }finally{
        try{
            rs.close();
            st.close();
          con.close();
        }catch(Exception e){ 
            JOptionPane.showConfirmDialog(null, e);
        }
        }

    add(myTable);
    myTable.setPreferredScrollableViewportSize(new Dimension(800,500));
    JScrollPane pane =new JScrollPane(myTable);
    add(pane,BorderLayout.CENTER );/*myTable.getRowHeight()* myTable.getRowCount())*/
    //myTable.repaint();

}

}

This executed fine but returns only one row from the database,multiple time(same as the number of rows in database) here's the screenshot of result and database table. Database result(data fetched)

please help..

but returns only one row from the database,multiple time

Because you only have a single row Vector.

You need a new Vector for each row of data:

    while(rs.next())
    {
        Vector<Object> row = new Vector<Object>();

        for(int j=1;j<columns;j++)
        {
            rows.addElement(rs.getString(j));
        }

        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