标题:[求助]请问如下问题怎么解决
只看楼主
wuzang
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2007-8-15
 问题点数:0 回复次数:0 
[求助]请问如下问题怎么解决
目的是实现从数据库中提取表格元素然后在窗体上显示一个表格 不明白获得表格内容那里有什么问题
public class Frame1 extends JFrame {

DefaultTableModel model;
GetConnection con = new GetConnection(); //调用连接数据库类
ResultSet rs=null;
ResultSetMetaData meta;

JPanel pane = new JPanel();
JTable table = new JTable();
JTableHeader jh = new JTableHeader();

/** Creates a new instance of Frame1 */
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void init() throws Exception {
pane = (JPanel) getContentPane();
pane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("JDBC");
model = new DefaultTableModel(table(),tableHeader());
jh.setBounds(27,10,342,20);
table.setModel(model);
table.setBounds(27,39,342,234);
pane.add(table);
pane.add(jh);
}
//获得表头
private Vector tableHeader(){
Vector vec=null;
try {
rs=con.select(rs);
meta = rs.getMetaData();
for (int i=1;i<meta.getColumnCount();i++){
vec.addElement(meta.getTableName(i));
}
}catch (SQLException ex) {
ex.printStackTrace();
}
return vec;
}
//获得表内容
private Vector table(){
Vector vec=null;
try{
rs=con.select(rs);
meta = rs.getMetaData();
while (rs.next()){
Vector v = null;
for (int i=1;i<meta.getColumnCount();i++){
v.addElement(rs.getObject(i)); //运行时此行报错
}
vec.addElement(v);
}
}catch (SQLException e){
e.printStackTrace();
}
return vec;
}

public static void main (String []args){
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Frame1 f=new Frame1();
f.setVisible(true);
}
});
}
}

--------------------------------------------------------------------------------------------------------
public class GetConnection {
Connection con=null;
PreparedStatement ps=null;
/**
* 连接数据库
*/
public Connection get(){
try {
String str = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(str);
String url = "jdbc:odbc:test";
con = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
......................................

//数据查询方法
public ResultSet select(ResultSet a){
try {
ps = get().prepareStatement("select * from student");
a = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a;
}
}

[此贴子已经被作者于2007-9-15 19:41:45编辑过]

搜索更多相关主题的帖子: 连接数据库 public null 元素 
2007-09-15 12:17



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-170121-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.413938 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved