所以,我试图插入从我的SQL Server(我使用的WamppServer)中检索到的数据,我注意到我所做的一切都不会出现在JTable中,即使数据是从服务器正确检索的。
我发布了整个代码,因为我认为我的问题在于Panel Creation,而不是数据插入。
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());
}
}
});
}
}
按下Button2时应用程序应打开一个新窗口并显示JTable(新窗口上的空格是JTable)。
我的电脑上的执行结果:
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
编辑:用于JTable填充的代码是从this接受的答案
复制而来的答案 0 :(得分:3)
这只是一列的粗略片段。如果可行,您可以将其扩展到所有列。 注意:未亲自测试。
DefaultTableModel model = new DefaultTableModel();
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/efefe?autoReconnect=true&useSSL=false","root","");
ResultSet rs = connection.createStatement().executeQuery("SELECT * FROM customer");
tableModel.addColumn("gender");
int row = 0;
while (rs.next()) {
tableModel.addRow(new Object[] {});
tableModel.setValueAt(rs.getString("gender"), row, 0);
row++;
}
table.setModel(tableModel);