How to view database resultset in Java swing? My options are
- jeditorpane
- jtable.
After view that file i want to save the file either in .rtf or .pdf. how is this possible in Java desktop apps?
Note: Do not use third party API or libraries
How to view database resultset in Java swing? My options are
After view that file i want to save the file either in .rtf or .pdf. how is this possible in Java desktop apps?
Note: Do not use third party API or libraries
JTable will do best, here is the code for display ResultSet on JTable
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
}
The method 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);
}
it would be hard to export data to pdf or rtf without using 3rd party api