我试图显示sql查询的结果" SELECT * FROM ..."在JFrame中。经过一些挖掘后,我在下面使用了一些代码,我在另一篇文章中找到了这些代码。我的问题是它没有在我的JFrame中显示Jtable。我是完全新手(这是我第一次尝试将guql与java结合使用)所以任何帮助都会非常感激......
框架代码:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class Frame extends JFrame {
public Frame() throws HeadlessException {
super();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Border loweredetched = null;
Font font = new Font("monospaced", Font.PLAIN, 11);
JPanel panel = new JPanel(new BorderLayout());
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JPanel lowerPanel = new JPanel();
panel.add(leftPanel, BorderLayout.WEST);
panel.add(rightPanel, BorderLayout.EAST);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
//JScrollPane scrollPane = new JScrollPane(GetSongs.table);
/*JTextArea text = new JTextArea(15, 3);
text.setMargin(new Insets(5, 5, 5, 5));
text.setBackground(Color.darkGray);
text.setForeground(Color.white);
text.setFont(font);
text.setEditable(false);*/
JButton button = new JButton("Update");
lowerPanel.add(button);
leftPanel.setBorder(BorderFactory.createTitledBorder(loweredetched, "Songs"));
leftPanel. add( new JScrollPane( GetSongs.table ), BorderLayout.CENTER );;
leftPanel.add(lowerPanel);
JTextArea textR = new JTextArea(1, 3);
textR.setMargin(new Insets(5, 5, 5, 5));
textR.setBackground(Color.darkGray);
textR.setForeground(Color.white);
textR.setFont(font);
textR.setEditable(false);
rightPanel.setBorder(BorderFactory.createTitledBorder(loweredetched, "ToBuy"));
rightPanel.add(textR);
getContentPane().add(panel, BorderLayout.NORTH);
pack();
}
public static void main(String[] args){
new Frame();
}
}
连接到db并进行查询的类的代码:
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
public class GetSongs extends JFrame {
// MAKES A QUERY TAKES THE RESULT SET AND PRODUCES A JTABLE
public static JTable table;
public GetSongs() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
String connectionURL = "jdbc:mysql://localhost:3306/songs";
Connection connection = null;
Statement statement = null;
//do not use in production!!
String dbuser = "root";
String dbpass = "";
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver").newInstance(); //
connection = DriverManager.getConnection(connectionURL, dbuser, dbpass);
statement = connection.createStatement();
String query = "SELECT * FROM songs JOIN purchases WHERE id = song_id and user_id =2;";
rs = statement.executeQuery(query);
table = new JTable(buildTableModel(rs));
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
System.out.println(columnCount);
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}
答案 0 :(得分:0)
作为替代方案,您可以使用SwingWorker。
您将在doInBackground()方法中检索所有数据库信息,然后在done()方法中填充表。
由于我无法向您解释,所以此处有更多信息:
http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html