import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class Data extends JFrame implements ActionListener{
Object a[][];
Object col[]={"姓名","性别","年龄","祖籍"};
JTable t;
JButton b1;
Container con;
Connection conn;
Statement sql;
ResultSet rs;
JProgressBar pbar;
Data(){
super("数据查询");
setSize(250,250);
setVisible(true);
a=new Object[40][4];
t=new JTable(a,col);
b1=new JButton("查询");
b1.addActionListener(this);
pbar=new JProgressBar(JProgressBar.VERTICAL,0,40);
pbar.setStringPainted(true);
con=getContentPane();
con.add(new JScrollPane(t),BorderLayout.CENTER);
con.add(b1,BorderLayout.SOUTH);
con.add(pbar,BorderLayout.WEST);
show();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
int i=0;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e1){
System.out.println("Can not link!");
}
try{
conn=DriverManager.getConnection("jdbc:odbc:test","hello","hello");
sql=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=sql.executeQuery(SELECT * FROM main);
while(rs.next()){
a[i][0]=rs.getString(1);
a[i][1]=rs.getString(2);
a[i][2]=String.valueOf(rs.getString(3));
a[i][3]=rs.getString(4);
i++;
pbar.setVisible(true);
pbar.setString("你已查询了"+i+"记录");
}
rs.close();
conn.close();
}
catch(SQLException e2){
System.out.println("Error!!!");
}
}
}
public static void main(String arg[]){
new Data();
}
}
这是上课讲的一个实例,我稍微改动了一下,我确实有一个数据库叫test,也有一个ID为hello的登陆,密码也为hello,同样,配置ODBC数据源test服务器 BILLGATES,帐号密码hello,测试也通过了.
但程序就是一直抛出SQL异常,我逐步测试一下,发现是 rs=sql.executeQuery("SELECT * FROM main");
这句出的抛出的这个异常,我的test数据库里确实存在一张名为main的表,百思不得奇解!~
请各位不吝赐教,即使解决不了能有一线索也是好的~.