-2

I want to extract data from database tables and show into a table from in JTable.
Here is some code i want to add the data from database base in array dataValues [] [] in this i store values in dataValues [][] static but i want to store values in dataValues[][] from the database table.

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 

Connection connection = DriverManager.getConnection(url, username, password); 
Statement statement = connection.createStatement(); 
String query = "select command_name, omc_name, to_module, start_time, end_time, status, priority, cmd_id from sync_task_table"; 

ResultSet resultset = statement.executeQuery(query); 

dm=new DefaultTableModel(); 

String columnNames[] = { "Column 1", "Column 2", "Column 3" }; 

// Create some data 
String dataValues[][] = { 
    { "12", "234", "67" }, 
    { "-123", "43", "853" }, 
    { "93", "89.2", "109" }, 
    { "279", "9033", "3092" } 
}; 

// Create a new table instance 
table = new JTable( dataValues, columnNames ); 

// Add the table to a scrolling pane 
scrollPane = new JScrollPane( table );
mihai
  • 2,746
  • 3
  • 35
  • 56
  • can't see there something about get the value from JDBC and to put to the TableModel – mKorbel Jul 09 '13 at 08:01
  • 1
    What exactly is your problem? – Daniel H. Jul 09 '13 at 08:03
  • yeah i want to get the values and store in dataValues[][].. but i dont know i to add values in dataValues[][] becoz i am not gud in 2D array to please kindly help me – user2508209 Jul 09 '13 at 08:04
  • possible duplicate of [How to fill data in a JTable with database?](http://stackoverflow.com/questions/2192764/how-to-fill-data-in-a-jtable-with-database) – kleopatra Jul 09 '13 at 08:09
  • 2
    what happened when you typed "java database swing" into the search field at the upper trailing edge of this (or any other) page of this site? Seriously, this question is asked a (felt) thousand times per week, please do some decent research before being the thousand-first ... – kleopatra Jul 09 '13 at 08:12

2 Answers2

3
  • read Oracle tutorial How to use Tables
  • reset DefaultTableModel to null, before a new Objects from JDBC are executed, added to DefaultTableModel, use (there are a few another methods, you can to starting with) model.setRowCount(0);
  • create DefaultTableModel, JTable as local variables, re_use those variables, don't to create a new those Objects, then is required to remove uselles Objects and add a new Objects to Swing GUI
  • override getColumnClass, its very important for JTables view to know data types stored in DefaultTableModel
  • create (I assumed that there isn't another column structure, if yes then multiplay this variables) ColumnModel as local variable
  • don't to reinvent the wheel, search for ResultSetTableModel, or better TableFromDatabase
mKorbel
  • 109,525
  • 20
  • 134
  • 319
3

First you should register the Driver in the preferred JDBC way

 Class.forName("oracle.jdbc.driver.OracleDriver");
 // instead of
 DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 

And now the answer to your question.

Initialize the dataValues via:

 String columnNames[] = { "Column 1", "Column 2", "Column 3" }; 
 List<Object[]> dataValueList = new ArrayList(); 

 while(resultset.next()){
    Object[] nextRow = new Object[columnNames.length];
    for(int col = 0; col < columnNames.length; col++){
        String colName = columnNames[col]
        Object colValue = resultset.getObject(colName);
        // do type conversion here - e.g. sql type to string
        nextRow[col] = colValue;
    }
    dataValueList.add(nextRow);
 }

 Object[][] dataValues = (Object[][]) dataValueList.toArray(new Object[dataValueList
            .size()][columnNames.length]);
René Link
  • 48,224
  • 13
  • 108
  • 140