JTABLE 怎样利用AbstractTableModel 接收数据库记录集并显示!??
谢谢
谢谢
解决方案 »
- 求教关于JList和ArrayList的问题
- 关于socket编程
- java问什么写入是乱码
- 请教JAVA实现GIS最短路径输出(带约束条件)
- jxl处理excel的问题
- jexcelapi拆分单元格怎么实现啊
- <html:options 标签的collection问题
- 在JFrame里的布局问题。
- 刚刚接触 Java ,所以问题有点蔡
- 书上说选择file|new frame为JBCL控件生成一个新窗体(由DecorateFrame产生),我怎么找不到decrateframe呢?
- 求救::如何在swing组件(如:jlabel)上设置一个超链接。就像html页面上的超链接一样!!
- java的Button问题
import java.sql.*;/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class TableModel extends AbstractTableModel {
private ResultSet rs;
private ResultSetMetaData rsmd;
public TableModel(ResultSet rs) {
this.rs = rs;
try {
this.rsmd = rs.getMetaData();
} catch (SQLException ex) {
ex.printStackTrace();
}
} public int getRowCount() {
try {
rs.last();
return rs.getRow();
} catch (SQLException ex) {
ex.printStackTrace();
return 0;
}
} public int getColumnCount() {
try {
return rsmd.getColumnCount();
} catch (Exception ex) {
ex.printStackTrace();
return 0;
}
} public String getColumnName(int columnIndex) {
try {
return rsmd.getColumnName(columnIndex + 1);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
} public Object getValueAt(int rowIndex, int columnIndex) {
try {
rs.absolute(rowIndex+1);
return rs.getObject(columnIndex + 1);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
import java.sql.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class SQLConnection {
private static Connection conn;
private static Statement stm;
private static ResultSet rs;
private static String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";
private static String Name="sa";
private static String Psw="713518";
private static String sql;
public SQLConnection() { }
public static ResultSet RsSelect(String sql){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn=DriverManager.getConnection(URL,Name,Psw);
stm=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
} catch (Exception ex) {
ex.printStackTrace();
}
try {
rs = stm.executeQuery(sql);
} catch (SQLException ex) {
ex.printStackTrace();
return null;
}
return rs;
}
public void RsClose(){
try{
conn.close();
stm.close();
rs.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
@version 1.1 2004-08-22
@author Cay Horstmann
*/import com.sun.rowset.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.sql.rowset.*;/**
This program shows how to display the result of a
database query in a table.
*/
public class ResultSetTable
{
public static void main(String[] args)
{
JFrame frame = new ResultSetFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}/**
This frame contains a combo box to select a database table
and a table to show the data stored in the table
*/
class ResultSetFrame extends JFrame
{
public ResultSetFrame()
{
setTitle("ResultSet");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); /* find all tables in the database and add them to
a combo box
*/ tableNames = new JComboBox();
tableNames.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
if (scrollPane != null) remove(scrollPane);
String tableName = (String) tableNames.getSelectedItem();
if (rs != null) rs.close();
String query = "SELECT * FROM " + tableName;
rs = stat.executeQuery(query);
if (scrolling)
model = new ResultSetTableModel(rs);
else
{
CachedRowSet crs = new CachedRowSetImpl();
crs.populate(rs);
model = new ResultSetTableModel(crs);
} JTable table = new JTable(model);
scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
validate();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
JPanel p = new JPanel();
p.add(tableNames);
add(p, BorderLayout.NORTH); try
{
conn = getConnection();
DatabaseMetaData meta = conn.getMetaData();
if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE))
{
scrolling = true;
stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
}
else
{
stat = conn.createStatement();
scrolling = false;
}
ResultSet tables = meta.getTables(null, null, null, new String[] { "TABLE" });
while (tables.next())
tableNames.addItem(tables.getString(3));
tables.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
} addWindowListener(new
WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
try
{
if (conn != null) conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
});
} /**
Gets a connection from the properties specified in
the file database.properties.
@return the database connection
*/
public static Connection getConnection()
throws SQLException, IOException
{
Properties props = new Properties();
FileInputStream in = new FileInputStream("database.properties");
props.load(in);
in.close(); String drivers = props.getProperty("jdbc.drivers");
if (drivers != null) System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password"); return DriverManager.getConnection(url, username, password);
} private JScrollPane scrollPane;
private ResultSetTableModel model;
private JComboBox tableNames;
private ResultSet rs;
private Connection conn;
private Statement stat;
private boolean scrolling; private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 300;
}/**
This class is the superclass for the scrolling and the
caching result set table model. It stores the result set
and its metadata.
*/
class ResultSetTableModel extends AbstractTableModel
{
/**
Constructs the table model.
@param aResultSet the result set to display.
*/
public ResultSetTableModel(ResultSet aResultSet)
{
rs = aResultSet;
try
{
rsmd = rs.getMetaData();
}
catch (SQLException e)
{
e.printStackTrace();
}
} public String getColumnName(int c)
{
try
{
return rsmd.getColumnName(c + 1);
}
catch (SQLException e)
{
e.printStackTrace();
return "";
}
} public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch (SQLException e)
{
e.printStackTrace();
return 0;
}
} public Object getValueAt(int r, int c)
{
try
{
rs.absolute(r + 1);
return rs.getObject(c + 1);
}
catch(SQLException e)
{
e.printStackTrace();
return null;
}
} public int getRowCount()
{
try
{
rs.last();
return rs.getRow();
}
catch(SQLException e)
{
e.printStackTrace();
return 0;
}
} private ResultSet rs;
private ResultSetMetaData rsmd;
}