good day guys...
i need some help here on how to populate a jtable with the data coming from ms access db. i'm using netbeans6.9. i made a code here but i cannot get it right.
please guys if anyone could help me on my problem, i would greatly appreciate it. thanks
here is my sample code:
[code]
public void PopulatejTable(){
String dbuser = "";
String dbpasswd = "";
String DriverPrefix = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\myWork2\\PersonalInfo\\db1.mdb";
String SQLString = ("select * from Table1");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception e){
JOptionPane.showMessageDialog(null,""+e.getMessage(),"JDBC DRIVER Error",JOptionPane.WARNING_MESSAGE);
return;
}
Statement stmt = null;
Connection con = null;
ResultSet rset = null;
try{
con = DriverManager.getConnection(DriverPrefix,dbuser,dbpasswd);
stmt = con.createStatement();
}catch(Exception e){
JOptionPane.showMessageDialog(null,""+e.getMessage(),"Connection Error",JOptionPane.WARNING_MESSAGE);
}
try{
rset = stmt.executeQuery(SQLString);
while(rset.next()){
jTable1.add(rset.getString(1),this);
jTable1.add(rset.getString(2),this);
jTable1.add(rset.getString(3),this);
jTable1.add(rset.getString(4),this);
}
con.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null,e.getMessage(),"Communication Error",JOptionPane.WARNING_MESSAGE);
}
}
[/code]
[code]
public final void DesignjTable(){
Connection con = null;
String SQLString = ("select * from Table1");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb));DBQ=C:\\myWork2\\PersonalInfo\\db1.mdb");
Statement stmt = con.createStatement();
ResultSet rset = stmt.executeQuery(SQLString);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(20);
jTable1.getColumnModel().getColumn(1).setPreferredWidth(100);
jTable1.getColumnModel().getColumn(2).setPreferredWidth(20);
jTable1.getColumnModel().getColumn(3).setPreferredWidth(20);
DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel();
dtm.setNumRows(0);
int li_row = 0;
while(rset.next()){
dtm.addRow(new Object[] {rset.getString("ID"),rset.getString("Username"),rset.getString("Password"),rset.getString("Website")});
jTable1.setValueAt(rset.getString(1), li_row, 0);
jTable1.setValueAt(rset.getString(2), li_row, 1);
jTable1.setValueAt(rset.getString(3), li_row, 2);
jTable1.setValueAt(rset.getString(4), li_row, 3);
li_row++;
}
}catch(Exception e){
}
}
[/code]
i'm using a 2 different kinds methods but still didn't lucky...
advance many thanks guys for help...
- Add new comment
- 3 views