0

Currently I have a standard SQL query in a String. I know exactly which column name fields to select to output into my JTable.

This should be easy.

But this is my question. For example, like on TOAD or SQLTools or Oracle Developer, when a user runs their SQL code (e.g. a select statement like SELECT * FROM tablename), how can I make sure whatever field columns are queried that it populates those names to my JTable?

At least a simple example to illustrate this would assist?

Thanks

StanislavL
  • 56,971
  • 9
  • 68
  • 98
NinjaLoop
  • 33
  • 8
  • *At least a simple example to illustrate this would assist?* See [this example](http://stackoverflow.com/questions/27049130/optimize-data-calling-in-jdbc-onto-jtable/27062173#27062173). – dic19 Feb 02 '15 at 11:42

1 Answers1

2

You can run the SQL query and obtain a ResultSet (JDBC). After that you can use the result set's metadata to obtain columns and create a TableModel for the JTable

See here and here

StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Hi, I am trying to use the code found on this site: http://technojeeves.com/index.php/9-freebies/22-resultset-to-tablemodel I created a new class, and implemented the code. Then, I am attempting to call the method from another class file inside a ActionPerformed method for a button called "Run Query". But it is not working... – NinjaLoop Feb 02 '15 at 13:26
  • `private void RunQueryButtonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: //////////// JTable 001 = 001(DbUtils.resultSetToTableModel(rs)); JTable t = new JTable(DbUtils.resultSetToTableModel(rs)); t.setVisible(true); t.setSize(300,400); /// table_001(DbUtils.resultSetToTableModel(rs)); }` – NinjaLoop Feb 02 '15 at 13:30
  • 1
    @NinjaLoop If you have a code snippet with your attempt, add it to your question instead of in a comment – Robin Feb 02 '15 at 17:36