I need to get all the values from a Database and put it inside the JTable, filling all rows. My code just puts inside the JTable only the last row and misses all previous. Anyone got an idea of how to get all the values from the DB and insert them all in the JTable?
try{
Class.forName("com.mysql.jdbc.Driver");
conn3 = DriverManager.getConnection("jdbc:mysql://localhost/test1?user=me&password=123456");
stmt3 = conn3.createStatement();
rs3 = stmt3.executeQuery("SELECT * FROM teams");
String str1 = null,str2 = null,str3 = null,str4 = null,str5 = null;
while (rs3.next())
{
str1 = rs3.getString(1);
str2 = rs3.getString(2);
str3 = rs3.getString(3);
str4 = rs3.getString(4);
str5 = rs3.getString(5);
}
Object[][] data = {{str1,str2,str3,str4,str5}};
String[] columnNames = {"id","Name","Players","Point","Position" };
table = new JTable(data,columnNames);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
table.setFillsViewportHeight(true);
table.setSurrendersFocusOnKeystroke(true);
table.setBounds(10, 321, 297, 143);
frame.getContentPane().add(table);
rs3.close();
stmt3.close();
conn3.close();
}
catch (SQLException se)
{
System.out.println( "SQL Exception:" );
while( se != null )
{
System.out.println( "State : " + se.getSQLState() );
System.out.println( "Message: " + se.getMessage() );
System.out.println( "Error : " + se.getErrorCode() );
se = se.getNextException();
}
}
catch (ClassNotFoundException ex)
{
System.out.println("Error: unable to load driver class!");
}