Total members 11895 |It is currently Sun Dec 22, 2024 3:15 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;

}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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     -  
 event results from the clicking of a button     -  
 PHP Google search results grabber.     -  
 Results of Java expressions for arithmetic operations     -  
 .aspx render block results (response.write, <%=     -  
 Re: what is the TCP/IP model?     -  
 OSI Model Layers     -  



Topic Tags

Java JDBC
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com