【发布时间】:2015-08-18 23:25:01
【问题描述】:
我正在尝试从我的数据库中获取一个表格。我遵循了一个教程,我的代码显示在下面,但是我收到了这个错误并且不知道为什么。在我的数据库中,我有“名字、姓氏、地址、城市、州、邮编”不确定是否需要这些信息来帮助我解决我的问题
有人可以帮忙
在此先感谢您的帮助。
package medicalrecords;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class TableFromDatabase extends JPanel {
private Connection conexao = null;
public TableFromDatabase() {
Vector columnNames = new Vector();
Vector data = new Vector();
try {
// Connect to an Access Database
conexao = DriverManager.getConnection("jdbc:derby://" + "localhost"
+ ":1527/Medical Records", "root", "password");
// Read data from a table
String sql = "select * from SD2799.PATIENTRECORDS";
try (Statement stmt = conexao.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++) {
columnNames.addElement(md.getColumnName(i));
}
// Get row data
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement(rs.getObject(i));
}
data.addElement(row);
}
}
conexao.close();
} catch (Exception e) {
System.out.println(e);
}
// Create table with database data
JTable table = new JTable(data, columnNames) {
@Override
public Class getColumnClass(int column) {
for (int row = 0; row < getRowCount(); row++) {
Object o = getValueAt(row, column);
if (o != null) {
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
JPanel buttonPanel = new JPanel();
add(buttonPanel, BorderLayout.SOUTH);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Patient Records");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableFromDatabase newContentPane = new TableFromDatabase();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
});
}
}
【问题讨论】:
-
注意:C:\Users\student\Desktop\CapstoneTuesday_SD2799\MedicalRecords\src\medicalrecords\TableFromDatabase.java 使用未经检查或不安全的操作。注意:使用 -Xlint:unchecked 重新编译以获取详细信息。哎呀哈哈忘了那部分