按下按钮后显示表格的问题,求高手!

新手小白 发布于 2015/12/09 15:10
阅读 89
收藏 0

学完AWT后写了个测试程序:

package demo;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class table2 extends JFrame{
/**

*/
private static final long serialVersionUID = 1L;
JTable tab;
JScrollPane sp;
public Button b1;
public JTextField jt1;
public JLabel jl1;
public JPanel jl;
Vector<String> rowdata;
Vector<String>header;
List<String>  her;

table2() throws IOException, SQLException, ClassNotFoundException{
super();
this.setSize(500,500);
this.setTitle("按姓名查找:");
this.setLayout(new BorderLayout());
   header=new Vector<String>(); 
   rowdata=new Vector<String>();
   String he[]={"姓名","收入","支出","余额"};
   for(int i=0;i<he.length;i++){
   header.add(he[i]);
    }
   List  her=new ArrayList ();
   for(int l=0;l<her.size();l++){
  rowdata.add((String) her.get(l));
  System.out.println(her.get(l));System.out.println(1);
   }
tab=new JTable(rowdata,header);
tab.setBackground(Color.BLUE);
    sp=new JScrollPane(tab);
jl=new JPanel();
jl.setLayout(new BorderLayout());
jl1=new JLabel("按姓名名称查询:");
jt1=new  JTextField();
jt1.setEditable(true);
b1=new Button("确定");
b1.addActionListener(new ac(jt1));
jl.add(jl1,BorderLayout.WEST);
jl.add(jt1,BorderLayout.CENTER);
jl.add(b1,BorderLayout.EAST);
this.add(sp,BorderLayout.CENTER);
this.add(jl,BorderLayout.SOUTH);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
 class ac implements ActionListener{
    JTextField d;
ac(JTextField d){
this.d=d;
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name=d.getText();
try {
Class.forName("com.mysql.jdbc.Driver");
   Connection kt;
kt = DriverManager.getConnection("jdbc:mysql://localhost:3306/bankcount","root","123");
Statement sm=kt.createStatement();
ResultSet rs = sm.executeQuery("select  *from informations where name = '"+name+"' ");
List her=new ArrayList();
while(rs.next()){
   her.add(rs.getString("name"));
her.add(rs.getInt("input"));
her.add(rs.getInt("output"));
her.add(rs.getInt("moneynow"));
}
}
catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

 }

 public static void main(String args[]) throws ClassNotFoundException, IOException, SQLException{
new table2();
}}
但是经过测试发现,这张表只有个表头,按下按钮后也没有用显示数据,我数据库里确实是有数据的。
加载中
返回顶部
顶部