当前位置: 技术问答>java相关
在JBuilder中如何使用网格控件?
来源: 互联网 发布时间:2015-04-14
本文导语: 在JBuilder中如何使用网格控件?并使其与数据库相连,显示数据库查询结果的数据? | 用表格控件不就成了? 我的例子如下: import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*...
在JBuilder中如何使用网格控件?并使其与数据库相连,显示数据库查询结果的数据?
|
用表格控件不就成了?
我的例子如下:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class ResultSetTable
{
public static void main(String[] args)
{ JFrame frame = new ResultSetFrame();
frame.show();
}
}
/* this class is the base class for the scrolling and the
caching result set table model. It stores the result set
and its metadata.
*/
abstract class ResultSetTableModel extends AbstractTableModel
{ public ResultSetTableModel(ResultSet aResultSet)
{ rs = aResultSet;
try
{ rsmd = rs.getMetaData();
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
public String getColumnName(int c)
{ try
{ return rsmd.getColumnName(c + 1);
}
catch(SQLException e)
{ System.out.println("Error " + e);
return "";
}
}
public int getColumnCount()
{ try
{ return rsmd.getColumnCount();
}
catch(SQLException e)
{ System.out.println("Error " + e);
return 0;
}
}
protected ResultSet getResultSet()
{ return rs;
}
private ResultSet rs;
private ResultSetMetaData rsmd;
}
/* this class uses a scrolling cursor, a JDBC 2 feature
*/
class ScrollingResultSetTableModel extends ResultSetTableModel
{ public ScrollingResultSetTableModel(ResultSet aResultSet)
{ super(aResultSet);
}
public Object getValueAt(int r, int c)
{ try
{ ResultSet rs = getResultSet();
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{ System.out.println("Error " + e);
return null;
}
}
public int getRowCount()
{ try
{ ResultSet rs = getResultSet();
rs.last();
return rs.getRow();
}
catch(SQLException e)
{ System.out.println("Error " + e);
return 0;
}
}
}
/* this class caches the result set data; it can be used
if scrolling cursors are not supported
*/
class CachingResultSetTableModel extends ResultSetTableModel
{ public CachingResultSetTableModel(ResultSet aResultSet)
{ super(aResultSet);
try
{ cache = new ArrayList();
int cols = getColumnCount();
ResultSet rs = getResultSet();
/* place all data in an array list of Object[] arrays
We don't use an Object[][] because we don't know
how many rows are in the result set
*/
while (rs.next())
{ Object[] row = new Object[cols];
for (int j = 0; j
我的例子如下:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class ResultSetTable
{
public static void main(String[] args)
{ JFrame frame = new ResultSetFrame();
frame.show();
}
}
/* this class is the base class for the scrolling and the
caching result set table model. It stores the result set
and its metadata.
*/
abstract class ResultSetTableModel extends AbstractTableModel
{ public ResultSetTableModel(ResultSet aResultSet)
{ rs = aResultSet;
try
{ rsmd = rs.getMetaData();
}
catch(SQLException e)
{ System.out.println("Error " + e);
}
}
public String getColumnName(int c)
{ try
{ return rsmd.getColumnName(c + 1);
}
catch(SQLException e)
{ System.out.println("Error " + e);
return "";
}
}
public int getColumnCount()
{ try
{ return rsmd.getColumnCount();
}
catch(SQLException e)
{ System.out.println("Error " + e);
return 0;
}
}
protected ResultSet getResultSet()
{ return rs;
}
private ResultSet rs;
private ResultSetMetaData rsmd;
}
/* this class uses a scrolling cursor, a JDBC 2 feature
*/
class ScrollingResultSetTableModel extends ResultSetTableModel
{ public ScrollingResultSetTableModel(ResultSet aResultSet)
{ super(aResultSet);
}
public Object getValueAt(int r, int c)
{ try
{ ResultSet rs = getResultSet();
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{ System.out.println("Error " + e);
return null;
}
}
public int getRowCount()
{ try
{ ResultSet rs = getResultSet();
rs.last();
return rs.getRow();
}
catch(SQLException e)
{ System.out.println("Error " + e);
return 0;
}
}
}
/* this class caches the result set data; it can be used
if scrolling cursors are not supported
*/
class CachingResultSetTableModel extends ResultSetTableModel
{ public CachingResultSetTableModel(ResultSet aResultSet)
{ super(aResultSet);
try
{ cache = new ArrayList();
int cols = getColumnCount();
ResultSet rs = getResultSet();
/* place all data in an array list of Object[] arrays
We don't use an Object[][] because we don't know
how many rows are in the result set
*/
while (rs.next())
{ Object[] row = new Object[cols];
for (int j = 0; j