我試圖以JTable
的形式顯示錶的數據。 我的表在Oracle 10g Express Edition
。 T.name:Doctor 我希望整個表格應該在JTable的幫助下以我的形式顯示。無法從JTable中的數據庫中獲取數據?
class PatientTableFromDatabase extends JFrame
{
static Connection con=null;
Statement st=null;
ResultSet rs=null;
PatientTableFromDatabase()
{
Vector columnNames = new Vector();
Vector data = new Vector();
try
{
// Connect to the Database
try{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//cn=DriverManager.getConnection("Jdbc:Odbc:pat");
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe", "hr", "hr");
}
catch(Exception e)
{
//System.out.println(e);
e.printStackTrace();
}
// Read data from a table
String sql = "Select * from Doctor";
Statement stmt = con.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);
}
}
catch(Exception e)
{
System.out.println(e);
//e.printStackTrace();
}
// Create table with database data
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane);
/* JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.SOUTH);*/
}
public static void main(String[] args)
{
PatientTableFromDatabase frame = new PatientTableFromDatabase();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
當我運行框架出現,但沒有數據,沒有表。 請幫忙!! 感謝
糾正,但隨後也同樣的事情,只出現幀,沒有桌子,在cmd中顯示java.lang.NullPointerException沒有data.And戒菸後。 – user3367768
@ user3367768打印堆棧跟蹤 – SpringLearner
我得到這個 java.lang.NullPointerException java.lang.NullPointerException at PatientTableFromDatabase。 at PatientTableFromDatabase.main –
user3367768