Bind Data to JTable

I'm using NetBeans 5.5 :

How could I bind data from table from MS Access DB into JTable ?!!

N.P. : I've made an application which reads data from the DB & use the following line of code to show the data .

System.out.append(rs.getString("Blob")

Now I want to display the data using some GUI ; JTable.

After searching ,I 've figured out that I'll use TableModel to bind the data from DB to JTable .

That's my working code ,How could I modify in it to display the data into JTable:

import javax.swing.*;

import java.sql.*;

import javax.swing.event.TableModelListener;

import javax.swing.table.TableModel;

publicclass MyConnection{

public MyConnection(){

}

publicstatic Connection getConnection()throws Exception{

Driver d = (Driver)Class.forName

("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

Connection c = DriverManager.getConnection(

"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Inventory.mdb"

);

return c;

}

//******************************************

static Connection theConn;

// public static String sql;

publicvoid retrieveData(String sql )

{

try{

// connection to an ACCESS MDB

theConn = MyConnection.getConnection();

ResultSet rs;

Statement stmt;

stmt = theConn.createStatement();

rs = stmt.executeQuery(sql);

boolean FirstLoop=true;

boolean DataExisted=false;

while (rs.next())

{

if(FirstLoop==true)

{

System.out.append("ID");

System.out.append("\t");

System.out.append("Product_Name");

System.out.append("\t");

System.out.append("Product_Price");

System.out.append("\t");

System.out.append("Amount_In_Stock");

System.out.append("\t");

System.out.println("");

FirstLoop=false;

DataExisted=true;

}

System.out.append(rs.getString("ID"));

System.out.append("\t\t");

System.out.append(rs.getString("Product_Name"));

System.out.append("\t\t");

System.out.append(rs.getString("Product_Price"));

System.out.append("\t\t");

System.out.println(rs.getString("Amount_In_Stock"));

}

if(DataExisted==false)

System.out.println("No Result is found ");

System.out.println("__ _");

System.out.println("__ _");

rs.close();

stmt.close();

}

catch (Exception e){

e.printStackTrace();

}

finally{

try{

if (theConn !=null) theConn.close();

}

catch (Exception e){

}

}

}

}

[4944 byte] By [Bugayaa] at [2007-11-27 9:17:57]
# 1

Well very good that your code is working. No all you need to do is get the JTable's model and instead of calling the System.out.println to display the data, place them in the table model. Here is some skeleton code you can tweak.

// after obtaining your result set

while(rs.next()) {

ResultSetMetadata rsm = rs.getMetaData();

Vector<Object> row = new Vector<Object>();

for(int i = 1; i <= rsm.getColumnCount(); i++) {

row.addElement( rs.getObject(i) );

}

((DefaultTableModel)yourTable.getModel()).addRow( row );

}

Doing something like this should put the data in your table model.

You can also try a search on the net, you'll probably come up with some good examples in less than 10 minutes

ICE

icewalker2ga at 2007-7-12 22:08:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for your help .

Excuse me But I'm beginner in java & I've further question ,

I've modified my code according to yours but I 've created Jtable by code ;

JTable Table=new JTable();

and in that line of code which fills the table model ,I've made that:

((DefaultTableModel)Table.getModel()).addRow( row );

After the while loop ,I 've added :

Table.show();

But that table wasn't displayed , why?!!

I 've checked the data in :

Vector<Object> row = new Vector<Object>();

it's holds the required data.

Bugayaa at 2007-7-12 22:08:47 > top of Java-index,Desktop,Core GUI APIs...
# 3

You obviously haven't read the Swing Tutorial(s). You should do that before attempting to even connect a database to a GUI App.

Read it here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Quick Example:

JTable table = new JTable();

JScrollPane pane = new JScrollPane( table );

JFrame frame = new JFrame("Application Window");

frame.getContentPane().add( pane );

frame.pack();

frame.setVisible(true);

ICE

icewalker2ga at 2007-7-12 22:08:47 > top of Java-index,Desktop,Core GUI APIs...
# 4
Simple example in this posting: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=5123381
camickra at 2007-7-12 22:08:47 > top of Java-index,Desktop,Core GUI APIs...