Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Database Cashing tables results model

Sat Apr 07, 2007 2:48 pm

This is a code i used in displaying the results of database queries in java as a JTable.
This Model will be the model of a new JTable Object.
What this model need in constructor is the Results set of statement results,


java code
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
*

*/
abstract class myResultTableModel extends AbstractTableModel
{
public myResultTableModel(ResultSet myResultSet)
{
rs=myResultSet;
try {

rsmd=myResultSet.getMetaData();
} catch (SQLException ex) {
ex.printStackTrace();
}

}

public String getColumnName(int Col)
{
try {
return rsmd.getColumnName(Col+1);
} catch (SQLException ex) {
ex.printStackTrace();
return "Unknown";
}
}

public int getColumnCount()
{
try {
return rsmd.getColumnCount();
} catch (SQLException ex) {
ex.printStackTrace();
return 0;
}
}
public ResultSet getResultSet()
{
return rs;
}

private ResultSet rs;
private ResultSetMetaData rsmd;
}
public class CashingResultTableModel extends myResultTableModel
{

public CashingResultTableModel(ResultSet rs)
{
super(rs);
myData=new ArrayList();
int Cols=getColumnCount();
ResultSet myresult=getResultSet();
try {

while(myresult.next())
{
Object[] row=new Object[Cols];

for(int j=0;j<row.length;j++)
row[j]=myresult.getObject(j+1);

myData.add(row);
}

} catch (SQLException ex) {
ex.printStackTrace();
}

}

public int getRowCount()
{

return myData.size();

}

public Object getValueAt(int rowIndex, int columnIndex)
{
if(rowIndex<myData.size())
{
return ((Object[])myData.get(rowIndex))[columnIndex];
}
else
return null;
}

private ArrayList myData;

}




Post a reply
  Related Posts  to : Database Cashing tables results model
 Optimize All Tables In A MySQL Database     -  
 Add two tables     -  
 Generating Tables from XML by php     -  
 Persisting a class into two tables     -  
 PHP Google search results grabber.     -  
 event results from the clicking of a button     -  
 Results of Java expressions for arithmetic operations     -  
 .aspx render block results (response.write, <%=     -  
 Re: what is the TCP/IP model?     -  
 m3g model from milkshape     -  

Topic Tags

Java JDBC