mysql+swing
工具/原料
mysql
mysql-connector-java-3.1.13-bin.jar
方法/步骤
1、创建mysql表student
2、创建student类
3、创建数据库连接及操作
4、创建事件监听
5、创建界面packagetest1;importjava.awt.Dimension;坡纠课柩importjavax.s臃扮哀坷wing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTable;importjava.awt.Rectangle;importjavax.swing.JScrollPane;importjava.util.Vector;importjava.sql.*;importjava.util.ArrayList;importjavax.swing.JLabel;importjavax.swing.JTextField;importjava.awt.Font;importjavax.swing.JButton;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JOptionPane;publicclassJTableAppFrameextendsJFrame{JPanelcontentPane;VectorCellsVector=newVector();VectorTitleVector=newVector();JScrollPanescp=newJScrollPane();JTabletab=null;DBCondbcon=newDBCon();JLabellbl_name=newJLabel();JLabellbl_age=newJLabel();JLabellbl_address=newJLabel();JTextFieldtxt_name=newJTextField();JTextFieldtxt_age=newJTextField();JTextFieldtxt_address=newJTextField();JButtonbtn_add=newJButton();JButtonbtn_del=newJButton();JButtonbtn_update=newJButton();JButtonbtn_reset=newJButton();publicJTableAppFrame(){try{setDefaultCloseOperation(EXIT_ON_CLOSE);jbInit();}catch(Exceptionexception){exception.printStackTrace();}}privatevoidjbInit()throwsException{contentPane=(JPanel)getContentPane();contentPane.setLayout(null);this.setResizable(false);setSize(newDimension(400,340));setTitle("JTable");scp.setBounds(newRectangle(46,32,297,157));lbl_name.setFont(newjava.awt.Font("宋体",Font.BOLD,12));lbl_name.setText("姓名:");lbl_name.setBounds(newRectangle(46,205,42,15));lbl_age.setFont(newjava.awt.Font("宋体",Font.BOLD,12));lbl_age.setText("年龄:");lbl_age.setBounds(newRectangle(200,205,42,15));lbl_address.setFont(newjava.awt.Font("宋体",Font.BOLD,12));lbl_address.setText("地址:");lbl_address.setBounds(newRectangle(46,232,42,15));txt_name.setFont(newjava.awt.Font("宋体",Font.BOLD,12));txt_name.setBounds(newRectangle(98,205,72,21));txt_age.setFont(newjava.awt.Font("宋体",Font.BOLD,12));txt_age.setBounds(newRectangle(250,205,72,21));txt_address.setFont(newjava.awt.Font("宋体",Font.BOLD,12));txt_address.setBounds(newRectangle(98,232,72,21));btn_add.setBounds(newRectangle(46,271,83,25));btn_add.setFont(newjava.awt.Font("宋体",Font.BOLD,12));btn_add.setText("添加");btn_add.addActionListener(newJTableAppFrame_btn_add_actionAdapter(this));btn_del.setBounds(newRectangle(155,271,83,25));btn_del.setFont(newjava.awt.Font("宋体",Font.BOLD,12));btn_del.setText("删除");btn_del.addActionListener(newJTableAppFrame_btn_del_actionAdapter(this));btn_update.setBounds(newRectangle(260,271,83,25));btn_update.setFont(newjava.awt.Font("宋体",Font.BOLD,12));btn_update.setText("修改");btn_update.addActionListener(newJTableAppFrame_btn_update_actionAdapter(this));btn_reset.setBounds(newRectangle(258,232,83,25));btn_reset.setFont(newjava.awt.Font("宋体",Font.BOLD,12));btn_reset.setText("重置");btn_reset.addActionListener(newJTableAppFrame_btn_reset_actionAdapter(this));contentPane.add(scp);contentPane.add(lbl_name);contentPane.add(lbl_age);contentPane.add(lbl_address);contentPane.add(txt_name);contentPane.add(txt_age);contentPane.add(txt_address);contentPane.add(btn_del);contentPane.add(btn_update);contentPane.add(btn_add);contentPane.add(btn_reset);dbcon.connectDB();this.showTable();tab=newJTable(CellsVector,TitleVector);scp.getViewport().add(tab);}publicvoidshowTable(){this.TitleVector.add("姓名");this.TitleVector.add("年龄");this.TitleVector.add("地址");dbcon.select();ArrayListlist=dbcon.select();for(inti=0;i<list.size();i++){Studentstu=(Student)list.get(i);Vectorv=newVector();v.add(stu.getName());v.add(stu.getAge());v.add(stu.getAddress());CellsVector.add(v);}}Stringname;Stringage;Stringaddress;publicintcheckInformation(){name=this.txt_name.getText();age=this.txt_age.getText();address=this.txt_address.getText();if(name.equals("")){JOptionPane.showMessageDialog(this,"您好!请输入姓名!","提示",1);this.txt_name.grabFocus();return0;}if(age.equals("")){JOptionPane.showMessageDialog(this,"您好!请输入年龄!","提示",1);this.txt_age.grabFocus();return0;}char[]ans=age.toCharArray();for(inti=0;i<ans.length;i++){if(!Character.isDigit(ans[i])){JOptionPane.showMessageDialog(this,"您好!年龄输入错误!","提示",1);this.txt_age.setText("");this.txt_age.grabFocus();return0;}}if(age.length()>3){JOptionPane.showMessageDialog(this,"您好!年龄最高只能为100岁!","提示",1);this.txt_age.setText("");this.txt_age.grabFocus();return0;}if(address.equals("")){JOptionPane.showMessageDialog(this,"您好!请输入地址!","提示",1);this.txt_address.grabFocus();return0;}ages=Integer.valueOf(age);return1;}intages;publicvoidbtn_add_actionPerformed(ActionEvente){if(this.checkInformation()==0){return;}dbcon.insert(name,ages,address);this.CellsVector.clear();this.showTable();this.tab.updateUI();this.btn_reset_actionPerformed(e);}ResultSetrs;publicvoidbtn_del_actionPerformed(ActionEvente){Stringname=this.txt_name.getText();this.txt_age.setEditable(false);this.txt_address.setEditable(false);if(name.equals("")){JOptionPane.showMessageDialog(this,"您好!请输入姓名!","提示",1);this.txt_name.grabFocus();return;}try{Stringsql="select*fromStudentwhereusername=?";PreparedStatementps=DBCon.con.prepareStatement(sql);ps.setString(1,name);rs=ps.executeQuery();if(rs.next()){ps.close();dbcon.delete(name);this.CellsVector.clear();this.showTable();this.tab.updateUI();this.btn_reset_actionPerformed(e);}else{JOptionPane.showMessageDialog(this,"您好!该学员不存在!","提示",1);this.btn_reset_actionPerformed(e);return;}}catch(SQLExceptionex){System.out.println("核对学员信息发生异常"+ex.getMessage());}}publicvoidbtn_update_actionPerformed(ActionEvente){if(this.checkInformation()==0){return;}try{Stringsql="select*fromStudentwhereusername=?";PreparedStatementps=DBCon.con.prepareStatement(sql);ps.setString(1,name);rs=ps.executeQuery();if(rs.next()){ps.close();dbcon.update(name,ages,address);this.CellsVector.clear();this.showTable();this.tab.updateUI();this.btn_reset_actionPerformed(e);}else{JOptionPane.showMessageDialog(this,"您好!该学员不存在!","提示",1);this.btn_reset_actionPerformed(e);return;}}catch(SQLExceptionex){System.out.println("核对学员信息发生异常"+ex.getMessage());}}publicvoidbtn_reset_actionPerformed(ActionEvente){this.txt_name.setText("");this.txt_age.setText("");this.txt_address.setText("");this.txt_age.setEditable(true);this.txt_address.setEditable(true);}}
6、创建调用
7、成功后