[英]How can I retrieve values from a DB and load them into a JTable?
我正在尝试从数据库加载值并将它们列出到 jtable 中。 我的语句正确执行,但是当我加载应用程序时,表空间中没有任何内容,也没有收到任何错误消息。 是否缺少声明? 我以前没有使用过 jtable。 我已经编辑了原始帖子以包含我的所有代码以引入上下文
//imports
public class viewMusic extends JFrame {
static Connection conn = null;
static viewMusic frame = new viewMusic();
private JPanel contentPane;
private JTable table;
private JTable table_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
viewMusic frame = new viewMusic();
//frame.setVisible(true);
try { //Login details for the DB.
String userName = "root";
String password = "";
String url = "jdbc:mysql://localhost/music";
conn = DriverManager.getConnection(url, userName, password);
//JOptionPane.showMessageDialog(null, "Connected");
} catch (Exception e) { //Error is DB connection is unsuccessful .
JOptionPane.showMessageDialog(null, "Cannot connect to database server");
// System.err.println(e.getMessage());
System.err.println(e.getStackTrace());
}
} catch (Exception e) {
e.printStackTrace();
}
Statement stmt;
try {
stmt = conn.createStatement();
DefaultTableModel model = new DefaultTableModel(new String[]{"Artist_Name", "Album_Title", "Record_Label", "Genre", "Year", "Format"}, 0);
JTable table = new JTable(model);
String query="SELECT * from cds";
System.out.println(query);
ResultSet rs = stmt.executeQuery(query);
String a = "artist";
String b = "album title";
String c = "record label";
String d = "genre";
String e = "year";
String f = "format";
model.addRow(new Object[]{a, b, c, d, e, f});
table.setModel(model);
JScrollPane scrollpane = new JScrollPane(table);
frame.getContentPane().add(scrollpane);
frame.setSize(600, 500);
frame.setVisible(true);
} catch (Exception e) { //Error is DB connection is unsuccessful .
JOptionPane.showMessageDialog(null, "Cannot connect");
// System.err.println(e.getMessage());
System.err.println(e.getStackTrace());
}
} //End of run()
});
} //End of main
/**
* Create the frame.
*/
public viewMusic() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table_1 = new JTable();
table_1.setBounds(25, 64, 551, 250);
contentPane.add(table_1);
JLabel lblStatus = new JLabel("Connection Status");
lblStatus.setBounds(399, 18, 149, 13);
contentPane.add(lblStatus);
lblStatus.setText("Connecting to DB..");
}
}
您需要将 JTable 添加到 JFrame
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class ViewMusic extends JFrame {
private static final long serialVersionUID = 1L;
// static Connection conn = null;
static ViewMusic frame = new ViewMusic();
private JPanel contentPane;
private static JTable table;
private static DefaultTableModel model;
// private JTable table_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
model = new DefaultTableModel(new Object[] { "Artist_Name",
"Album_Title", "Record_Label", "Genre", "Year", "Format" },
0);
String a = "artist";
String b = "album title";
String c = "record label";
String d = "genre";
String e = "year";
String f = "format";
model.addRow(new Object[] { a, b, c, d, e, f });
table.setModel(model);
frame.repaint();
}
});
} // End of main
/**
* Create the frame.
*/
public ViewMusic() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table = new JTable();
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setBounds(25, 64, 551, 250);
contentPane.add(scrollpane);
JLabel lblStatus = new JLabel("Connection Status");
lblStatus.setBounds(399, 18, 149, 13);
contentPane.add(lblStatus);
lblStatus.setText("Connecting to DB..");
setVisible(true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.