企业🤖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"); //参数中的true为打开新的窗口,父窗口不可编辑 Tianjia tj=new Tianjia(this,"添加学生信息",true); xsxx2=new Xsxx(); //更新jtable bg1.setModel(xsxx2); } else if(e.getActionCommand().equals("xiugai")) { //System.out.println("ccc"); int ii=this.bg1.getSelectedRow(); if(ii==-1) { JOptionPane.showMessageDialog(this,"请选中要修改的行"); return; } new Xiugai(this,"修改学生信息",true,xsxx2,ii); xsxx2=new Xsxx(); bg1.setModel(xsxx2); } else if(e.getActionCommand().equals("shanchu")) { //System.out.println("ddd"); int ii=this.bg1.getSelectedRow();//选中的行 if(ii==-1) { JOptionPane.showMessageDialog(this,"请选中要删除的行");//弹出信息界面 return;//返回调用的地方 } String st=(String)xsxx2.getValueAt(ii,0);//返回时object,需要强转为字符串,0是因为主键是第0列(习惯上的第一列,即xuehao) PreparedStatement ps=null; Connection ct=null; //声明Connection对象 ResultSet rs=null;//声明ResultSet类,用来存放获取的结果集!! Statement sm=null; try { Class.forName("com.mysql.jdbc.Driver"); ct=DriverManager.getConnection("jdbc:mysql://122.14.200.136:3306/javadb","root","6a133f0024"); ps=ct.prepareStatement("delete from xuesheng where xuehao=?"); ps.setString(1,st);//给第一个问号赋值 ps.executeUpdate(); } catch (Exception e2){} finally { try { if(rs!=null) { rs.close(); } if(ps!=null) { ps.close(); } if(ct!=null) { ct.close(); } } catch (Exception e3){} } xsxx2=new Xsxx(); bg1.setModel(xsxx2); } } } ~~~ 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){} } } } ~~~ Tianjia.java ~~~ package studysystem; import javax.swing.JDialog; import javax.swing.JFrame; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; class Tianjia extends JDialog implements ActionListener { JLabel bq1,bq2,bq3,bq4,bq5,bq6; JTextField wbk1,wbk2,wbk3,wbk4,wbk5,wbk6; JButton an1,an2; JPanel mb1,mb2,mb3,mb4; public Tianjia(Frame fck,String ckm,Boolean msck) { super(fck,ckm,msck); bq1=new JLabel(" 学号 "); bq2=new JLabel(" 姓名 "); bq3=new JLabel(" 性别 "); bq4=new JLabel(" 年龄 "); bq5=new JLabel(" 籍贯 "); bq6=new JLabel(" 院系 "); wbk1=new JTextField(5); wbk2=new JTextField(5); wbk3=new JTextField(5); wbk4=new JTextField(5); wbk5=new JTextField(5); wbk6=new JTextField(5); an1=new JButton("添加"); an1.addActionListener(this); an1.setActionCommand("tianjia2"); an2=new JButton("取消"); an2.addActionListener(this); an2.setActionCommand("quxiao"); mb1=new JPanel(); mb2=new JPanel(); mb3=new JPanel(); mb4=new JPanel(); mb1.setLayout(new GridLayout(6,1)); mb2.setLayout(new GridLayout(6,1)); mb1.add(bq1); mb1.add(bq2); mb1.add(bq3); mb1.add(bq4); mb1.add(bq5); mb1.add(bq6); mb2.add(wbk1); mb2.add(wbk2); mb2.add(wbk3); mb2.add(wbk4); mb2.add(wbk5); mb2.add(wbk6); mb3.add(an1); mb3.add(an2); this.add(mb1,BorderLayout.WEST); this.add(mb2); this.add(mb3,BorderLayout.SOUTH); this.add(mb4,BorderLayout.EAST); this.setSize(370,270); this.setLocation(401,281); this.setResizable(false); //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("tianjia2")) { PreparedStatement ps=null; Connection ct=null; //声明Connection对象 ResultSet rs=null;//声明ResultSet类,用来存放获取的结果集!! Statement sm=null; try { //加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); ct=DriverManager.getConnection("jdbc:mysql://122.14.200.136:3306/javadb","root","6a133f0024"); String ss=("insert into xuesheng values(?,?,?,?,?,?)"); ps=ct.prepareStatement(ss); ps.setString(1,wbk1.getText()); ps.setString(2,wbk2.getText()); ps.setString(3,wbk3.getText()); ps.setString(4,wbk4.getText()); ps.setString(5,wbk5.getText()); ps.setString(6,wbk6.getText()); ps.executeUpdate(); this.dispose(); } catch (Exception e2){} finally { try { if(rs!=null) { rs.close(); } if(ps!=null) { ps.close(); } if(ct!=null) { ct.close(); } } catch (Exception e3){} } } else if(e.getActionCommand().equals("quxiao"))//取消 { this.dispose(); } } } ~~~ Xiugai.java ~~~ package studysystem; import javax.swing.JDialog; import javax.swing.JFrame; import java.awt.*; import java.awt.event.*; import java.sql.*; import java.util.*; import javax.swing.*; class Xiugai extends JDialog implements ActionListener { JLabel bq1,bq2,bq3,bq4,bq5,bq6; JTextField wbk1,wbk2,wbk3,wbk4,wbk5,wbk6; JButton an1,an2; JPanel mb1,mb2,mb3,mb4; public Xiugai(Frame fck,String ckm,Boolean msck,Xsxx xsxx2,int hang) { super(fck,ckm,msck); bq1=new JLabel(" 学号 "); bq2=new JLabel(" 姓名 "); bq3=new JLabel(" 性别 "); bq4=new JLabel(" 年龄 "); bq5=new JLabel(" 籍贯 "); bq6=new JLabel(" 院系 "); wbk1=new JTextField(5); wbk1.setText((String)xsxx2.getValueAt(hang,0)); wbk1.setEditable(false); wbk2=new JTextField(5); wbk2.setText((String)xsxx2.getValueAt(hang,1)); wbk3=new JTextField(5); wbk3.setText((String)xsxx2.getValueAt(hang,2)); wbk4=new JTextField(5); wbk4.setText((String)xsxx2.getValueAt(hang,3).toString()); wbk5=new JTextField(5); wbk5.setText((String)xsxx2.getValueAt(hang,4)); wbk6=new JTextField(5); wbk6.setText((String)xsxx2.getValueAt(hang,5)); an1=new JButton("修改"); an1.addActionListener(this); an1.setActionCommand("xiugai"); an2=new JButton("取消"); an2.addActionListener(this); an2.setActionCommand("quxiao"); mb1=new JPanel(); mb2=new JPanel(); mb3=new JPanel(); mb4=new JPanel(); mb1.setLayout(new GridLayout(6,1)); mb2.setLayout(new GridLayout(6,1)); mb1.add(bq1); mb1.add(bq2); mb1.add(bq3); mb1.add(bq4); mb1.add(bq5); mb1.add(bq6); mb2.add(wbk1); mb2.add(wbk2); mb2.add(wbk3); mb2.add(wbk4); mb2.add(wbk5); mb2.add(wbk6); mb3.add(an1); mb3.add(an2); this.add(mb1,BorderLayout.WEST); this.add(mb2); this.add(mb3,BorderLayout.SOUTH); this.add(mb4,BorderLayout.EAST); this.setSize(370,270); this.setLocation(401,281); this.setResizable(false); //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("xiugai")) { PreparedStatement ps=null; Connection ct=null; //声明Connection对象 ResultSet rs=null;//声明ResultSet类,用来存放获取的结果集!! Statement sm=null; try { Class.forName("com.mysql.jdbc.Driver"); ct=DriverManager.getConnection("jdbc:mysql://122.14.200.136:3306/javadb","root","6a133f0024"); String ss=("update xuesheng set xingming=?,xingbie=?,nianling=?,jiguan=?,yxmc=? where xuehao=?"); ps=ct.prepareStatement(ss); ps.setString(1,wbk2.getText()); ps.setString(2,wbk3.getText()); ps.setString(3,wbk4.getText()); ps.setString(4,wbk5.getText()); ps.setString(5,wbk6.getText()); ps.setString(6,wbk1.getText()); ps.executeUpdate(); this.dispose(); } catch (Exception e2){} finally { try { if(rs!=null) { rs.close(); } if(ps!=null) { ps.close(); } if(ct!=null) { ct.close(); } } catch (Exception e3){} } } else if(e.getActionCommand().equals("quxiao")) { this.dispose(); } } } ~~~ ![](https://box.kancloud.cn/123c199e6c62df7b77f8d1bfde3a7d9d_501x399.png) ![](https://box.kancloud.cn/2aa94d6a3335e0ac096436e927476432_609x405.png)