So, I am trying to insert data retrieved from my SQL Server (i used WamppServer) and I noticed that everything I do wont appear in the JTable, even though the data is correctly retrieved from the server.
I am posting the whole code because I assume my issue is with the Panel Creation and not from the data insertion.
import java.awt.*;
import java.lang.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class MyWindow {
private JFrame frame,frame2,frame3,frame4;
private JTextArea textArea_3,textArea_3_2;
public static Connection connection = null;
public static Statement statement = null;
public static String sql = null;
public static ResultSet rs = null;
public JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyWindow window = new MyWindow();
window.frame.setVisible(true);
connection = null;
statement = null;
try
{
System.out.println("conneting to Database...");
Class.forName("com.mysql.jdbc.Driver"); // Register JDBC driver
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/efefe?autoReconnect=true&useSSL=false","root",""); // Open connection
System.out.println("Connection Successful");
}
catch(ClassNotFoundException error)
{
System.out.println("Error:" + error.getMessage());
}
catch(SQLException error)
{
System.out.println("Error:" + error.getMessage());
}
finally
{
if (connection != null)
try {
connection.close();
}
catch(SQLException ignore)
{
}
if (statement != null)
try {
statement.close();
}
catch(SQLException ignore)
{
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
}
public MyWindow() {
initialize();
}
public void initialize() {
frame = new JFrame();
frame3 = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("BUTTON2");
btnNewButton.setBounds(133, 21, 121, 23);
frame.getContentPane().add(btnNewButton);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
frame3 = new JFrame(); // frame 3 is the new opening window
frame3.setVisible(true);
frame3.setBounds(200, 300, 550, 300);
frame3.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // no need to close the whole app, just the new window
frame3.getContentPane().setLayout(null);
try
{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/efefe?autoReconnect=true&useSSL=false","root",""); // Open connection
statement = connection.createStatement();
sql = "SELECT * FROM customer";
rs = statement.executeQuery(sql);
while (rs.next()) {
int code = rs.getInt("code");
String gender = rs.getString("gender");
String birthday = rs.getString("birthday");
String status = rs.getString("status");
table = new JTable();
table.setBounds(15, 123, 500, 120);
frame3.getContentPane().add(table);
table.setVisible(true);
ResultSetMetaData metaData = rs.getMetaData();
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
System.out.println("DATA RETRIEVED FROM SERVER");
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
System.out.println(vector);
}
System.out.println("JTable has " + table.getRowCount() + " rows!"); // 0 = empty
System.out.println("JTable has " + table.getColumnCount() + " columns!"); // 0 = empty
}
rs.close();
statement.close();
connection.close();
}
catch(SQLException error)
{
System.out.println("Error:" + error.getMessage());
}
}
});
}
}
The app is supposed to open a new window when Button2 is pressed and show the JTable(that white space on the new window is the JTable).
Execution Results on my PC:
conneting to Database...
Connection Successful
DATA RETRIEVED FROM SERVER
[102, female, 1995-02-24, single]
[103, male, 1985-12-04, married]
[104, female, 1987-10-08, married]
[105, female, 1977-09-01, married]
[106, male, 1994-05-30, single]
[107, female, 1990-06-30, single]
[108, male, 1994-07-24, single]
[109, female, 1991-05-10, single]
[110, male, 2001-07-12, single]
JTable has 0 rows!
JTable has 0 columns
EDIT: Code used for JTable filling was copied from this accepted answer