日期:2014-05-20  浏览次数:20901 次

(Jtable与SQL2000)看看代码 为什么没显示出来数据表的信息
菜鸟新手啊 我是怎么想的 在Jtable里设置好与数据表的连接 直接显示表 这样行么 为什么 表显示了 里面都是空的 先谢过了啊
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

import javax.swing.*;
public class abc extends JFrame 
{

Connection con;
Statement stmt;
ResultSet rs;
Object a[][];
JButton bt_save;
int i=0;
Object header[]={"学号","姓名","性别","年龄","邮箱"};


abc()
{
a=new Object[9][5];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{
System.out.println("Error:"+e);
}
try{
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hy";  
String user = "hy";  
String password = "sa"; 
Connection con = DriverManager
.getConnection(url,user,password);
 
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from Tqc");
while(rs.next())
{
a[i][0]=rs.getInt(1);
a[i][1]=rs.getString(2);
a[i][2]=rs.getString(3);
a[i][3]=rs.getInt(4);
a[i][4]=rs.getString(5);
i++;
}
}
catch(SQLException e)
{
System.out.println("SqlError:"+e);
}
JTable table=new JTable(a,header);
Container con=getContentPane();
getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
setSize(400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
validate();
}



public static void main(String args[])
{
new abc();
}
}





------解决方案--------------------
1.检查下你rs.next是否有值。可以在里面打印信息。
2.stmt=con.createStatement();就可以了。
3.记得关闭数据库连接。