0

How to view database resultset in Java swing? My options are

  1. jeditorpane
  2. 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

3
  • if you want with 3rd party then i could tell you Commented Jul 31, 2012 at 7:02
  • What have you done so far? Any research any code? Commented Jul 31, 2012 at 7:05
  • See also AbstractTableModel GUI display issue. Commented Apr 15, 2023 at 12:38

2 Answers 2

2

Viewing your result - JTable is your best choice

Saving the results, well, unless you want to use a 3rd part library, you options are very limited, unless your very familiar with the various file formats you want to use.

Out of the box, I'd say CVS would be easy to achieve.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.