[SOLVED] Cant display data in jTable
I tried to display data from database to jTable. This is what i try and i cant display the data. Can anyone tell me where is the problem?
Code:
public class NewJPanel extends javax.swing.JPanel {
static JTable myTable;
/** Creates new form NewJPanel */
public NewJPanel() {
initComponents();
myTable = new JTable(3, 2);
JScrollPane myPane = new JScrollPane(myTable,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(myPane);
myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
}
public static void main(String args[]) throws SQLException {
JFrame myFrame = new JFrame("Table");
myFrame.getContentPane().add(new NewJPanel());
myFrame.setVisible(true);
myFrame.pack();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jigsaw puzzle", "root", "MP037");
Statement stmt = null;
ResultSet rs = null;
String cuser = "SELECT user_id, score_easy FROM score ";
stmt = connection.createStatement();
rs = stmt.executeQuery(cuser);
int li_row = 0;
while (rs.next()) {
myTable.setValueAt(rs.getString(1), li_row, 0);
myTable.setValueAt(rs.getString(2), li_row, 1);
li_row++;
}
}
use any tablemodel to display data in table
public class NewJPanel extends javax.swing.JPanel {
static JTable myTable;
/** Creates new form NewJPanel */
public NewJPanel() {
initComponents();
myTable = new JTable(3, 2);
JScrollPane myPane = new JScrollPane(myTable,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(myPane);
myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
}
public static void main(String args[]) throws SQLException {
JFrame myFrame = new JFrame("Table");
myFrame.getContentPane().add(new NewJPanel());
myFrame.setVisible(true);
myFrame.pack();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jigsaw puzzle", "root", "MP037");
Statement stmt = null;
ResultSet rs = null;
String cuser = "SELECT user_id, score_easy FROM score ";
stmt = connection.createStatement();
rs = stmt.executeQuery(cuser);
DefaultTableModel resultData = null;
final Object[] columnNames=new String[] {"user_id","score_easy"};
resultData = new DefaultTableModel(columnNames,0);
int li_row = 0;
while (rs.next()) {
resultData.addRow(new Vector());
resultData.setValueAt(rs.getString(1), li_row, 0);
resultData.setValueAt(rs.getString(2), li_row, 1);
li_row++;
}
myTable..setModel(resultData);
}