主题:java 数据库操作
请教一下这个代码错在哪里,查询结果显示在表格里!谢谢!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class SearchRecord extends JDialog implements ActionListener{
JLabel Lable;
JTextField number;
Object name[]={"考生编号","姓名","性别","出生日期","年龄","报考学校","报考专业","联系电话"};
Object a[][]=new Object[1][8];
JTable table;
JButton enter;
Connection con;
Statement sql;
ResultSet rs;
String num;
int k=4;
SearchRecord(String s){
setTitle(s);
Lable=new JLabel("输入考生编号:");
number=new JTextField(10);
number.setText("1001");
a=new Object[4][8];
table=new JTable(a,name);
enter=new JButton("查询记录");
Box baseBox=Box.createHorizontalBox();
baseBox.add(Lable);
baseBox.add(number);
baseBox.add(enter);
getContentPane().add(baseBox,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
enter.addActionListener(this);
setBounds(250,350,600,200);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==enter){
try{ con=DriverManager.getConnection("jdbc:odbc:hello","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM message WHERE 'number'='"+number+"'");
int k=0;
a[k][0]=rs.getString(1);
a[k][1]=rs.getString(2);
a[k][2]=rs.getString(3);
a[k][3]=rs.getString(4);
a[k][4]=rs.getString(5);
a[k][5]=rs.getString(6);
a[k][6]=rs.getString(7);
a[k][7]=rs.getString(8);
con.close();
}
catch(SQLException ee){ System.out.println(ee);}
table=new JTable(a,name);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
validate();
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class SearchRecord extends JDialog implements ActionListener{
JLabel Lable;
JTextField number;
Object name[]={"考生编号","姓名","性别","出生日期","年龄","报考学校","报考专业","联系电话"};
Object a[][]=new Object[1][8];
JTable table;
JButton enter;
Connection con;
Statement sql;
ResultSet rs;
String num;
int k=4;
SearchRecord(String s){
setTitle(s);
Lable=new JLabel("输入考生编号:");
number=new JTextField(10);
number.setText("1001");
a=new Object[4][8];
table=new JTable(a,name);
enter=new JButton("查询记录");
Box baseBox=Box.createHorizontalBox();
baseBox.add(Lable);
baseBox.add(number);
baseBox.add(enter);
getContentPane().add(baseBox,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
enter.addActionListener(this);
setBounds(250,350,600,200);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==enter){
try{ con=DriverManager.getConnection("jdbc:odbc:hello","","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=sql.executeQuery("SELECT * FROM message WHERE 'number'='"+number+"'");
int k=0;
a[k][0]=rs.getString(1);
a[k][1]=rs.getString(2);
a[k][2]=rs.getString(3);
a[k][3]=rs.getString(4);
a[k][4]=rs.getString(5);
a[k][5]=rs.getString(6);
a[k][6]=rs.getString(7);
a[k][7]=rs.getString(8);
con.close();
}
catch(SQLException ee){ System.out.println(ee);}
table=new JTable(a,name);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
validate();
}
}
}