ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
添加监听,完成查询功能 Test.java ~~~ package studysystem; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; public class Test extends JFrame implements ActionListener { JPanel mb1,mb2; JLabel bq1;//标签 JTextField wbk1;//文本框 JButton an1,an2,an3,an4;//按钮 JTable bg1; //表格 JScrollPane gd1;//滚动条 Xsxx xsxx2; public static void main(String[] args) { Test xs=new Test(); } public Test() { mb1=new JPanel(); bq1=new JLabel("请输入姓名"); wbk1=new JTextField(10); an1=new JButton("查询"); an1.addActionListener(this); an1.setActionCommand("chaxun"); mb1.add(bq1); mb1.add(wbk1); mb1.add(an1); mb2=new JPanel(); an2=new JButton("添加"); an2.addActionListener(this); an2.setActionCommand("tianjia"); an3=new JButton("修改"); an3.addActionListener(this); an3.setActionCommand("xiugai"); an4=new JButton("删除"); an4.addActionListener(this); an4.setActionCommand("shanchu"); mb2.add(an2); mb2.add(an3); mb2.add(an4); xsxx2=new Xsxx(); bg1=new JTable(xsxx2); //JTable 后面的括号先记录后字段 //bg1=new JTable(jilu,ziduan); gd1=new JScrollPane(bg1); this.add(gd1); this.add(mb1,"North"); this.add(mb2,"South"); this.setTitle("学生管理系统"); this.setSize(500,400); this.setLocation(201,181); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("chaxun")) { //System.out.println("aaa"); String xingming=this.wbk1.getText().trim(); String sql="select * from xuesheng where xingming='"+xingming+"'"; xsxx2=new Xsxx(sql); bg1.setModel(xsxx2); } else if(e.getActionCommand().equals("tianjia")) { System.out.println("bbb"); } else if(e.getActionCommand().equals("xiugai")) { System.out.println("ccc"); } else if(e.getActionCommand().equals("shanchu")) { System.out.println("ddd"); } } } ~~~ Xsxx.java ~~~ package studysystem; import java.sql.*; import java.util.*; import javax.swing.table.*; class Xsxx extends AbstractTableModel { Vector ziduan,jilu;//字段和记录两个集合类 PreparedStatement ps=null; Connection ct=null; //声明Connection对象 ResultSet rs=null;//声明ResultSet类,用来存放获取的结果集!! public int getRowCount() { return this.jilu.size(); } public int getColumnCount() { return this.ziduan.size(); } public Object getValueAt(int hang, int lie) { return ((Vector)this.jilu.get(hang)).get(lie); } public Xsxx() { this.sqlyj("select * from xuesheng"); } public Xsxx(String ss) { this.sqlyj(ss); } public String getColumnName(int e) { return (String)this.ziduan.get(e); } public void sqlyj(String sql) { ziduan=new Vector(); ziduan.add("学号"); ziduan.add("姓名"); ziduan.add("性别"); ziduan.add("年龄"); ziduan.add("籍贯"); ziduan.add("所在院系"); jilu=new Vector(); try { //加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); ct=DriverManager.getConnection("jdbc:mysql://122.14.200.136:3306/javadb","root","6a133f0024"); ps=ct.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ Vector hang=new Vector(); hang.add(rs.getString(1)); hang.add(rs.getString(2)); hang.add(rs.getString(3)); hang.add(rs.getInt(4)); hang.add(rs.getString(5)); hang.add(rs.getString(6)); jilu.add(hang); } } catch (Exception e){} finally { try { if(rs!=null) { rs.close(); } if(ps!=null) { ps.close(); } if(ct!=null) { ct.close(); } } catch (Exception e){} } } } ~~~ ![](https://box.kancloud.cn/5532207168c885c9e4ded8c41f4faaec_502x396.png)