[求助]请问如下问题怎么解决
目的是实现从数据库中提取表格元素然后在窗体上显示一个表格 不明白获得表格内容那里有什么问题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编辑过]