我想從我的數據庫中獲取一個表來填充。我跟着一個教程,我的代碼顯示在下面,但我得到這個錯誤,不知道爲什麼。在我的數據庫我的第一個名字,姓氏,地址,城市,州,郵編「不知道,如果需要此信息來幫助我與我的問題在java中顯示數據庫表
可能有人請幫助
感謝你在先進的你的幫助。
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);
});
}
}
什麼是錯誤? – Reimeus
注意:C:\ Users \ student \ Desktop \ CapstoneTuesday_SD2799 \ MedicalRecords \ src \ medicalrecords \ TableFromDatabase.java使用未經檢查或不安全的操作。 注意:使用-Xlint重新編譯:取消選中以獲取詳細信息。 Opps lol忘記了那部分 – Jessica