access数据库查询内容在table中显示
比如说我有一个ACCESS的数据库,
已经连接并查询到数据,保存在一个ResultSet类型的变量RS中,
我想要把这个变量RS还原成表格的形式显示出来,
结果中只有两个属性“num”“name”,都是文本格式;
因为不熟悉表格,这段代码自己写不来,请高手帮忙,
最好包括定义表格这部分的代码,谢谢!
可以发到我的电子邮箱:ebinge323@vip.sina.com!
------解决方案--------------------给你个例子参考下吧 ACCESS数据库不是真正意义上的,如果有游标移动部分的肯定不支持,自己修改吧~~
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)