Java数据库查询显示表格问题
Java数据库查询显示表格,我想从数据库中查询员工表中的“员工编号”、“员工姓名”,“联系电话”,但是显示不出表格
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class SqlEmployee extends JFrame implements ActionListener {
public static void main (String args[]){
SqlEmployee sqlEmployee=new SqlEmployee();
}
JTable table;
Object a[][];
Object name[]={"员工编号","员工姓名","联系电话"};
JTextField number;
int initnumber=1;
JPanel pNorth;
SqlEmployee(){
number=new JTextField(10);
number.addActionListener(this);
pNorth=new JPanel();
setTitle("查询员工信息");
pNorth.add(new JLabel("输入员工ID,回车确定"));
pNorth.add(number);
add(pNorth,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
setBounds(300,400,550,250);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){ //监听器
initnumber=Integer.parseInt(number.getText());
a=new Object[0][3];
table=new JTable(a,name);
table.setRowHeight(20);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
add(pNorth,BorderLayout.NORTH);
validate();
try
{
String url="jdbc:mysql://localhost/coldwine";
String user="root";
String pwd="";
//加载驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立到MySQL的连接
Connection conn = DriverManager.getConnection(url,user, pwd);
//执行SQL语句
Statement stmt = conn.createStatement();//创建语句对象,用以执行sql语言
ResultSet rs = stmt.executeQuery("select * from employee where ID='initnumber'");
//处理结果集
while (rs.next()) //主要问题是这里查询后显示不出表格,求高手指点!!!
{
for(int j=0;j<3;j++)
{
a[0][j]=new String[0][3];
}
a[0][0]=rs.getString(1);
a[0][1]=rs.getString(2);
a[0][2]=rs.getString(3);
table=new JTable(a,name);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
add(pNorth,BorderLayout.NORTH);
validate();
}
rs.close();//关闭数据库
conn.close();
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
麻烦高手帮忙修改下程序,在线等,第一个采纳修改正确的程序!万分感谢!!!
------解决方案--------------------public void actionPerformed(ActionEvent e){ //监听器
initnumber=Integer.parseInt(number.getText());
a=new Object[0][3];
此处 ,new 了个0行3列的数组??
应该是new Object[1][3]吧。。
------解决方案--------------------"select * from employee where ID='initnumber'" 这里好像也错了吧!
"select * from employee where ID="+initnumber
------解决方案--------------------
把aa,bb,cc 替换成你想要的数据....
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;