2012-07-31 28 views
0

如何在Java swing中查看數據庫結果集?我的選擇是如何在Java swing中查看數據庫結果集

  1. 的JEditorPane
  2. JTable中。

查看該文件後,我想保存文件在.rtf.pdf。這在Java桌面應用程序中如何實現?

注:不要使用第三方API或庫

+1

這功課嗎? – 2012-07-31 06:47:39

+0

如果你想與第三方,那麼我可以告訴你 – 2012-07-31 07:02:18

+0

你到目前爲止做了什麼?任何研究任何代碼? – Sap 2012-07-31 07:05:09

回答

2

查看你的結果 - 的JTable是您的最佳選擇

保存結果,那麼,除非你想使用第三部分庫,選項非常有限,除非您非常熟悉您要使用的各種文件格式。

開箱,我會說CVS很容易實現。

+0

或CSV? :)快樂的編碼! – dbalakirev 2012-07-31 06:56:28

+0

@ dave00與PDF格式相同:P – MadProgrammer 2012-07-31 06:58:57

2

JTable會做最好的,這裏是顯示ResultSetJTable

public static void main(String[] args) throws Exception { 
// The Connection is obtained 

ResultSet rs = stmt.executeQuery("select * from product_info"); 

// It creates and displays the table 
JTable table = new JTable(buildTableModel(rs)); 
JOptionPane.showMessageDialog(null, new JScrollPane(table)); // or can you other swing component 

// Closes the Connection 
} 

的方法buildTableModel代碼:

public static DefaultTableModel buildTableModel(ResultSet rs) 
    throws SQLException { 

ResultSetMetaData metaData = rs.getMetaData(); 

// names of columns 
Vector<String> columnNames = new Vector<String>(); 
int columnCount = metaData.getColumnCount(); 
for (int column = 1; column <= columnCount; column++) { 
    columnNames.add(metaData.getColumnName(column)); 
} 

// data of the table 
Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 
while (rs.next()) { 
    Vector<Object> vector = new Vector<Object>(); 
    for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) { 
     vector.add(rs.getObject(columnIndex)); 
    } 
    data.add(vector); 
} 

return new DefaultTableModel(data, columnNames); 

} 

這將是很難出口數據PDFrtf不使用第三方API

相關問題