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){
}
}
}
}

