比如说我有一个ACCESS的数据库,
已经连接并查询到数据,保存在一个ResultSet类型的变量RS中,
我想要把这个变量RS还原成表格的形式显示出来,
结果中只有两个属性“num”“name”,都是文本格式;
因为不熟悉表格,这段代码自己写不来,请高手帮忙,
最好包括定义表格这部分的代码,谢谢!
可以发到我的电子邮箱:[email protected]!
已经连接并查询到数据,保存在一个ResultSet类型的变量RS中,
我想要把这个变量RS还原成表格的形式显示出来,
结果中只有两个属性“num”“name”,都是文本格式;
因为不熟悉表格,这段代码自己写不来,请高手帮忙,
最好包括定义表格这部分的代码,谢谢!
可以发到我的电子邮箱:[email protected]!
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;
}
还有更好的解答吗,如果没有的话,我就把分都给他了!
看看也可以
将rs 放在request里面(request.setAttribute("rs",rs);
然后forward(假设你以了解mvc)
到jsp页面<% page contentType="text/html;charset=gbk" %>
<% page import="java.sql.ResultSet" %>
<table>
<tr>
<th>num
<th>name
<%
ResultSet rs = (ResultSet)request.getAttribute("rs");
while(rs.next){
%>
<tr>
<td><%=rs.getString(1)%>
<td><%=rs.getString(2)%>
<%}
rs.close;
%>
</table>
最好在servelt里 把rs里数据取出来 在forward
不用这摸麻烦 还不知lz有没用servlet
直接在这里调用查询的方法返回rs再 <%=rs.getString(index)%>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
public class TestTable{
Frame f=new Frame("use table display data");
Button querybutton=new Button("使用表格输出数据");
Connection connection=null;
Statement statement=null;
ResultSet rs=null;
String title[]={"学号","姓名","性别","年龄"};
Vector vector=new Vector();
AbstractTableModel tm;
JTable table;
JScrollPane scroll;
public TestTable(){
initTable();
init();
}
public void init(){
f.setSize(500,500);
f.setLayout(new FlowLayout());
querybutton.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
querybutton_actionperformed(e);
}
});
f.add(querybutton);
f.setVisible(true);
f.setResizable(false);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void initTable(){
tm=new AbstractTableModel(){
public int getColumnCount(){
return title.length;
}
public int getRowCount(){
return vector.size();
}
public Object getValueAt(int row,int column){
if(!vector.isEmpty()){
return ((Vector) vector.elementAt(row)).elementAt(column);
}else{
return null;
}
}
public void setValueAt(Object value,int row,int column){
}
public String getColumnName(int column){
return title[column];
}
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row,int column){
return false;
}
};
table=new JTable(tm);
table.setToolTipText("use table display data");
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
table.setAutoResizeMode(table.AUTO_RESIZE_ALL_COLUMNS);
table.setCellSelectionEnabled(false);
scroll=new JScrollPane(table);
scroll.setBounds(21,10,619,200);
f.add(scroll);
}
public void querybutton_actionperformed(ActionEvent e){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=studentdatabase.mdb");
statement=connection.createStatement();
rs=statement.executeQuery("select * from studentinformation");
vector.removeAllElements();
tm.fireTableStructureChanged();
while(rs.next()){
Vector vector1=new Vector();
vector1.addElement(String.valueOf(rs.getString("学号")));
vector1.addElement(rs.getString("姓名"));
vector1.addElement(rs.getString("性别"));
vector1.addElement(String.valueOf(rs.getString("年龄")));
vector.addElement(vector1);
}
rs.close();
}catch(SQLException ex){
ex.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
statement.close();
connection.close();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}
public static void main(String args[]){
TestTable t=new TestTable();
}}