• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable

 
tanvir rony
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use JTable in front end(AbstractTableModel).
Oracle in the back end.
i pass data through a form to the database, i want to retrive and show data in the JTable which is in my that form.
please provide me the code.
thanks.
 
Sayed Ibrahim Hashimi
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please provide me the code....
I don't think anyone will just give you the code.
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have connected to your database, you may create JTable as follow:
class myModel extends AbstractTableModel
{
String []colNames;
String []rowData;
int colCount;
Vector data = new Vector();
//You have to pass in this class Statement
//object of your connection
Statement stmt;
public myModel(Statement stmt)
{
this.stmt = stmt;
//You mast create ResultSet and
//ResultSetMetaData object for
//receiving datas from your table
try
{
ResultSet rs = stmt.exequteQuery ("Selct * from myTable");
ResultSetMetaData rsmd = rs.getMetaData();
colCount = rsmd.getColumnCount();
colNames = new String[colCount];
for(int i=0; i<colCount; i++)
{
colNames[i] = rsmd.getColumnName(i);
}
while(rs.next())
{
rowData = new String[colCount];
for(int i=0; i<colCount; i++)
{
rowData[i]=rs.getString(i+1);
}
data.add(rowData);
rowData = null;
}
}
catch(SQLException sqle)
{System.out.println(sqle.getMessage());}
}
//Now you must overload methods from
// AbstactTableModel
public int getColumnCount()
{return colCount;}
public String getColumnName(int col)
{return colNames[col];}
public int getRowCount()
{return data.size();}
public Object getValueAt(int row, int col)
{return ((String[])(data.elementAt(row)))[col];}
}
That's all.
Now in your application you may create an object of myModel type, and when you will create a JTable object, you will pass it myMidel object as argument.
I hope it will help You and sorry for my English;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic