我目前正在NetBeans IDE中開發一個程序。我創建了一個很好的GUI,並且創建了我的MS Access數據庫。我在JTable
中顯示MS Access數據時遇到問題。我想避免使用矢量,正如我在互聯網上發現的大多數教程所顯示的,因爲我仍然在高中,而且這種知識超出了我的視野。如何在JTable中顯示來自MS Access數據庫的數據?
任何正確的方向指針將非常感激!
這裏是我的代碼:
String[] columnNames = {"First Name",
"Last Name",
"Category",
"Amount"
};
Object[] row =new Object[4];
JLabel lbl=new JLabel("Add New Property");
lbl.setBounds(100,200,200,100);
lbl.setVisible(true);
invntryfrm.add(lbl);
//invntryfrm.setVisible(true);
JPanel panel=new JPanel();
panel.setBounds(20,200,680,100);
panel.setBackground(Color.WHITE);
invntrybck.add(panel);
DefaultTableModel model=new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
JTable tabel=new JTable();
tabel.setBounds(100,20,700,400);
tabel.setBackground(Color.DARK_GRAY);
tabel.setForeground(Color.WHITE);
tabel.setModel(model);
tabel.setPreferredScrollableViewportSize(new Dimension(500,50));
tabel.setFillsViewportHeight(true);
JScrollPane pane=new JScrollPane(tabel);
panel.add(pane);
try{
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\MUHAMMAD SHAHAB\\real estate.accdb");
String sql="select Username,Password,Country,City from simba";
PreparedStatement pst=conn.prepareStatement(sql);
ResultSet rs=pst.executeQuery();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
請參考回答@ http://stackoverflow.com/questions/27679867/jtable-how-to-use-rs2xml你需要使用rs2xml.jar。這是在jTable中呈現結果集的最簡單方法。 –
*「我有問題..」*確切地說有什麼問題?你可以在命令行應用程序中檢索數據嗎?你可以用硬編碼數據創建表格嗎?現在是更多細節的好時機,而不是更少。一般提示:1)爲了更快地提供更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 2)Java GUI必須在不同的語言環境中使用不同的PLAF來處理不同的操作系統,屏幕大小,屏幕分辨率等。 .. –
..因此,它們不利於像素的完美佈局。請使用佈局管理器或[它們的組合](http://stackoverflow.com/a/5630271/418556)以及[white space]的佈局填充和邊框(http://stackoverflow.com/a/17874718/ 418556)。 3)我會在star-up中添加表格,並且在執行DB查詢後簡單地創建並設置模型。在創建GUI之後添加組件顯示出它自己的一系列挑戰。 –