Connecting to Microsoft access thru jtable
Hi Programmers!!
Happy New Year!!
I am taking efforts to develop a jtable(right now of 3 cols) in such a
maner.
1. by default it has a single row.
2. user can enter values in it.
3. when he hits enter after writing the value for the last cell second row gets added to the model and also the values from the table are inserted in the database.
4. user can also fetch the records from the database, modify and also delete te records.
this is the code which i am trying the same
package table;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.*;
publicclass Class1extends JFrame
{
JScrollPane jScrollPane1 =new JScrollPane();
JTable editableTable1 =new JTable(1, 3);
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
Statement st=null;
public Class1()
{
try
{
DefaultCellEditor defCellEditor =new DefaultCellEditor(new JTextField());
TableColumn tc = editableTable1.getColumnModel().getColumn(2);
tc.setCellEditor(defCellEditor);
defCellEditor.addCellEditorListener(new TableCellListener());
this.getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(25, 19, 330, 180));
this.getContentPane().add(jScrollPane1,null);
jScrollPane1.getViewport().add(editableTable1,null);
addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
catch(Exception e)
{
System.out.println("Exception in Class1:"+e);
}
}
class TableCellListenerimplements CellEditorListener
{
publicvoid editingStopped(ChangeEvent e)
{
DefaultTableModel tableModel = (DefaultTableModel) editableTable1.getModel();
//System.out.println("number of rows initially are"+tableModel.getRowCount());
if(editableTable1.getSelectedRow() == tableModel.getRowCount() - 1)
{
int n=tableModel.getRowCount();
System.out.println("Get row count "+n);
insert(tableModel,n);
tableModel.setRowCount(tableModel.getRowCount() + 1);
}
}
publicvoid insert(DefaultTableModel tableModel,int i)
{
int n=i;
System.out.println("row count in insert func "+n);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Got Driver");
con=DriverManager.getConnection("jdbc:odbc:class");
System.out.println("Got Connection");
String a=(String)tableModel.getValueAt(n-1, 0);
System.out.println("string a "+a);
String b=(String)tableModel.getValueAt(n-1, 1);
System.out.println("string b "+b);
String c=(String)tableModel.getValueAt(n-1, 2);
System.out.println("string c "+c);
String qry="insert into tab values(?,?,?)";
pst=con.prepareCall(qry);
pst.setString(1, (String)tableModel.getValueAt(n-1, 0));
pst.setString(2, (String)tableModel.getValueAt(n-1, 1));
pst.setString(3, (String)tableModel.getValueAt(n-1, 2));
System.out.println("Created Prepared Statement");
pst.close();
con.close();
}
catch(SQLException t)
{
System.out.println("Exception generated "+t);
t.printStackTrace();
}catch (ClassNotFoundException p)
{
p.printStackTrace();
}
}
publicvoid editingCanceled(ChangeEvent e)
{
}
}
publicstaticvoid main(String[] args)
{
Class1 class1 =new Class1();
class1.setSize(400, 400);
class1.setVisible(true);
}
}
my gmail id adisusns
yahoo idadityaunhale
email adisuns@gmail.com
pl help me
[6664 byte] By [
adi_007a] at [2007-11-26 13:46:00]

# 1
Add a TableModelListener to the TableModel. It will fire an event whenever a cell is updated. Your code should then check to make sure that a value has been entered for all columns. Then you can update the database.
But then you need to decide what happens when a user changes a cell because you can no longer just do an insert, but instead you would need to do an update.
# 2
Thanks ! I wud try it!Can u tell me what is ClassDefNotFound?and how to tackle it?
# 3
You can't find the .class file. Recompile your code to generate the .class file.
# 4
i have te class file. i am using eclipse.give me ur emai sothat i can send u and u can see wath is happningmail; me at adisuns@gmail.com
# 5
> i have te class file.Then your class path is set up incorrectly> i am using eclipse.I don't, so I can't help you.
# 6
do u have some examples of tablemodellistener? i am mot understanding it. i am facing the problem of classdefnotfound in the the project of sudoku. it is showing exceptions for system.out.print() statement. i am also using jcreatot and dos. wat u use?
# 7
> do u have some examples of tablemodellistener?
http://forum.java.sun.com/thread.jspa?forumID=57&threadID=566133
I don't use an IDE. I just compile from the command line.
Here are the basics:
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html
# 8
Boss!!!
I tried a lott but didi not got the results pl chk the code.
I added the tableModelListener pl help me.
package table;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.*;
public class Class1 extends JFrame
{
JScrollPane jScrollPane1 = new JScrollPane();
JTable table = new JTable(1, 3);
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
Statement st=null;
public Class1()
{
try
{
DefaultCellEditor defCellEditor = new DefaultCellEditor(new JTextField());
TableColumn tc = table.getColumnModel().getColumn(2);
tc.setCellEditor(defCellEditor);
defCellEditor.addCellEditorListener(new TableCellListener());
//1def1CellEditor.addCellEditorListener(this);
this.getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(25, 19, 330, 180));
this.getContentPane().add(jScrollPane1, null);
jScrollPane1.getViewport().add(table, null);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
catch(Exception e)
{
System.out.println("Exception in Class1:"+e);
}
}
class TableCellListener implements CellEditorListener ,TableModelListener
{
public void editingStopped(ChangeEvent e)
{
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.addTableModelListener(this);
//System.out.println("number of rows initially are"+tableModel.getRowCount());
if(table.getSelectedRow() == tableModel.getRowCount() - 1)
{
int n=tableModel.getRowCount();
System.out.println("Get row count "+n);
//tableChanged(e);
tableModel.setRowCount(tableModel.getRowCount() + 1);
}
}
public void tableChanged(TableModelEvent e)
{
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.addTableModelListener(this);
//System.out.println("control in table changed event ");
//System.out.println("number of rows initially are"+tableModel.getRowCount());
/*if(table.getSelectedRow() == tableModel.getRowCount() - 1)
{
int n=tableModel.getRowCount();
System.out.println("Get row count "+n);
//tableChanged(e);
tableModel.setRowCount(tableModel.getRowCount() + 1);
}*/
//System.out.println(e.getSource());
if (e.getType() == TableModelEvent.INSERT)
{
int row=e.getFirstRow();
System.out.println("e.getfirstrow "+row);
int col = e.getColumn();
//System.out.println("e.getcol \n"+col);*/
//while (col==0&& col == 1 && col == 2)
//{
TableModel model = table.getModel();
System.out.println("row count in insert func "+row);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Got Driver");
con=DriverManager.getConnection("jdbc:odbc:class");
System.out.println("Got Connection");
//Stringa = model.getValueAt(row, 0).toString();
System.out.println("string a "+(String)model.getValueAt(row, 0));
//Stringb = model.getValueAt(row, 1); gives output as null
//System.out.println("string b "+b);
Stringc = model.getValueAt(row, 2).toString();
System.out.println("string c "+c);
/*String qry="insert into tab values(?,?,?) ";
pst=con.prepareCall(qry);
pst.setObject(1, model.getValueAt(row, 1).toString());
pst.setObject(2,model.getValueAt(row, 0));
pst.setObject(3, model.getValueAt(row, 2));
System.out.println("Created Prepared Statement");
pst.close();
con.close();*/
}
catch(SQLException t)
{
System.out.println("Exception generated "+t);
t.printStackTrace();
} catch (ClassNotFoundException p)
{
p.printStackTrace();
}
//}*/
}
}
public void editingCanceled(ChangeEvent arg0)
{
}
/*public void insert(DefaultTableModel tableModel,int i)
{
int n=i;
System.out.println("row count in insert func "+n);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Got Driver");
con=DriverManager.getConnection("jdbc:odbc:class");
System.out.println("Got Connection");
String a=(String)tableModel.getValueAt(n-1, 0);
System.out.println("string a "+a);
String b=(String)tableModel.getValueAt(n-1, 1);
System.out.println("string b "+b);
String c=(String)tableModel.getValueAt(n-1, 2);
System.out.println("string c "+c);
String qry="insert into tab values(?,?,?) ";
pst=con.prepareCall(qry);
pst.setObject(1, a);
pst.setObject(2, b);
pst.setObject(3, c);
System.out.println("Created Prepared Statement");
pst.close();
con.close();
}
catch(SQLException t)
{
System.out.println("Exception generated "+t);
t.printStackTrace();
} catch (ClassNotFoundException p)
{
p.printStackTrace();
}
}*/
}
public static void main(String[] args)
{
Class1 class1 = new Class1();
class1.setSize(400, 400);
class1.setVisible(true);
}
}
# 9
> I added the tableModelListener pl help me. Your code looks nothing like my example. a) you add the listener to the model when the table is created. b) There is not need to use a custom cell editor.
# 10
can you pl corect the code?i am not understanding the concept.whole of my project is dependant on such tablespl help
# 11
> can you pl corect the code?
I gave you a working example of how a TableModelListener works. An event is fired every time the contents of a cell changes. So when you receive this notification you do your required processing.
In my simple example if you change the contents of column 1 or 2, the value of column 3 is recalculated. I don't know how to make the example any simpler.
If my example doesn't help, then I guess I don't understand your problem.
# 12
ur cod has no problem. It has solved my other problem.....
what i want from the code which i have send is this.....
there is a blank row........ initialy
there are three colms A B &C(how did thet got there names i donot know)
user types in all coloums ... then he reaches in the last cel of C coloum
when he hits enter a new row gets added (this is working in my code).
Also what ever data he has added SHOULD BE INSERTED IN THE "tab" table which is in microsoft access. (this is creating me a problem)
i have tried to collect the values using
tableModel.getDataVector()
i can see the value using S.O.P(); statement.
ths is the output of my code
Get row count 1
Table model contains [[one, two, three]]
Get row count 2
Table model contains [[one, two, three], [four, five, six]]
Get row count 3
Table model contains [[one, two, three], [four, five, six], [seven , eight nine, nine]]
when the row count is 2
[[one, two, three]] shud be replaced by[four, five, six]]
and should be inserted in that table.
sorry to mofify your code
package table;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.*;
import java.util.Vector;
public class Class2 extends JFrame
{
JScrollPane jScrollPane1 = new JScrollPane();
JTable table = new JTable(1, 3);
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
Statement st=null;
Vector v=new Vector();
public Class2()
{
try
{
DefaultCellEditor defCellEditor = new DefaultCellEditor(new JTextField());
TableColumn tc = table.getColumnModel().getColumn(2);
tc.setCellEditor(defCellEditor);
defCellEditor.addCellEditorListener(new TableCellListener());
this.getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(25, 19, 330, 180));
this.getContentPane().add(jScrollPane1, null);
jScrollPane1.getViewport().add(table, null);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
catch(Exception e)
{
System.out.println("Exception in Class1:"+e);
}
}
class TableCellListener implements CellEditorListener
{
public void editingStopped(ChangeEvent e)
{
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
if(table.getSelectedRow() == tableModel.getRowCount() - 1)
{
int n=tableModel.getRowCount();
System.out.println("Get row count "+n);
//System.out.println("vector found "+(tableModel.getDataVector()));
System.out.println("Table model contains "+tableModel.getDataVector());
// System.out.println("String s contains "+s[n]);*/
tableModel.getDataVector().clear();
tableModel.setRowCount(tableModel.getRowCount() + 1);
}
}
public void editingCanceled(ChangeEvent arg0)
{
}
}
public static void main(String[] args)
{
Class2 class2 = new Class2();
class2.setSize(400, 400);
class2.setVisible(true);
}
}
# 13
Hey i got it!!!!
package table;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;
import java.sql.*;
import java.util.Vector;
public class Class2 extends JFrame
{
JScrollPane jScrollPane1 = new JScrollPane();
JTable table = new JTable(1, 3);
Connection con=null;
PreparedStatement pst=null;
ResultSet rs=null;
Statement st=null;
Vector v=new Vector();
int i=0,x,y;
String[] s=new String[3];
public Class2()
{
try
{
DefaultCellEditor defCellEditor = new DefaultCellEditor(new JTextField());
TableColumn tc = table.getColumnModel().getColumn(2);
tc.setCellEditor(defCellEditor);
defCellEditor.addCellEditorListener(new TableCellListener());
this.getContentPane().setLayout(null);
jScrollPane1.setBounds(new Rectangle(25, 19, 330, 180));
this.getContentPane().add(jScrollPane1, null);
jScrollPane1.getViewport().add(table, null);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
}
catch(Exception e)
{
System.out.println("Exception in Class1:"+e);
}
}
class TableCellListener implements CellEditorListener
{
public void editingStopped(ChangeEvent e)
{
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
if(table.getSelectedRow() == tableModel.getRowCount() - 1)
{
int n=tableModel.getRowCount();
System.out.println("\nGet row count "+n);
System.out.println("Table model contains "+tableModel.getDataVector().get(i));
v=(Vector)tableModel.getDataVector().get(i);
System.out.println("Vector V contains "+v);
v.copyInto(s);
for(int h=0;h<3;h++)
{
System.out.println(" "+s[h]);
}
String a =s[0];
System.out.println("String a = "+a);
String b =s[1];
System.out.println("String b = "+b);
String c =s[2];
System.out.println("String C = "+c);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Got Driver");
con=DriverManager.getConnection("jdbc:odbc:class");
System.out.println("Got Connection");
String qry="insert into tab values(?,?,?); ";
pst=con.prepareCall(qry);
pst.setString(1,a );
pst.setString(2,b );
pst.setString(3,c );
int k=pst.executeUpdate();
System.out.println("Created Prepared Statement "+k);
pst.close();
con.close();
}
catch(SQLException t)
{
System.out.println("Exception generated "+t);
t.printStackTrace();
} catch (ClassNotFoundException p)
{
p.printStackTrace();
}
tableModel.setRowCount(tableModel.getRowCount() + 1);
i++;
}
}
public void editingCanceled(ChangeEvent arg0)
{
}
}
public static void main(String[] args)
{
Class2 class2 = new Class2();
class2.setSize(400, 400);
class2.setVisible(true);
}
}
1 problem solved. more are on the list
hope u guide me!!!
