日期:2014-05-20 浏览次数:20769 次
自己刚开始学Java的时候做的一个学生管理系统,虽然现在看来比较菜,但对于刚入JAVA殿堂的同学来说,应该还是有帮助的,现在拿来和大家分享,希望对于初学者有所帮助. 1.StudentInfo类 import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; /* *操作员登录窗口 */ public class StudentInfo extends JFrame { private static final long serialVersionUID = 1L; Connection conn = null; Statement stmt = null; ResultSet rs = null; JTextField text1 = new JTextField(16); JTextField text2 = new JPasswordField(16); public StudentInfo() { setTitle("学生信息管理系统"); setSize(250, 180); setLocation(300, 400); Container cp = getContentPane(); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); JLabel label = new JLabel("登录"); label.setFont(new Font("Serif", Font.BOLD, 20)); JButton button = new JButton("确定"); JButton button1 = new JButton("取消"); //button(确定)的监听器 button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { try{ Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo?useUnicode=true&characterEncoding=gb2312","root","root"); stmt = conn.createStatement(); String str1 = text1.getText(); String str2 = text2.getText(); rs = stmt.executeQuery("select * from user where usename = '"+str1+"' and password = '"+str2+"'"); if(rs.next()) { new IndexFrame(); dispose(); } else { JOptionPane.showMessageDialog(null, "登录不正确!"); } } catch (ClassNotFoundException cnf){ cnf.printStackTrace(); } catch (SQLException sql){ sql.printStackTrace(); } finally{ try{ rs.close(); stmt.close(); conn.close(); } catch(SQLException sql){ sql.printStackTrace(); } } } }); // button(取消)的监听器 button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); p1.setLayout(new FlowLayout()); p2.setLayout(new FlowLayout()); p1.add(label); p2.add(button); p2.add(button1); cp.add(p1,BorderLayout.NORTH); cp.add(p2,BorderLayout.SOUTH); JLabel lable1 = new JLabel("用户名:"); JLabel lable2 = new JLabel("密 码:"); p3.add(lable1); p3.add(text1); p3.add(lable2); p3.add(text2); cp.add(p3); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setResizable(false); setVisible(true); } public static void main(String[] args) { new StudentInfo(); } } 2.IndexFrame类 import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; /* * 程序主窗口 */ public class IndexFrame extends JFrame { private static final long serialVersionUID = 1L; private ConnServer cs = new ConnServer(); private Container cp = getContentPane(); private JTable table = new JTable(); private DefaultTableModel defaultModel; Vector<String> name = new Vector<String>(); public static void main(String[] args) { new IndexFrame(); } public IndexFrame() { setTitle("学生信息管理"); setSize(600,500); setLocation(200, 100); setLayout(new BorderLayout()); JButton b1 = new JButton("添加"); b1.addActionListener(new AdddataAction()); JButton b2 = new JButton("删除"); b2.addActionListener(new DeleteAction()); JButton b3 = new JButton("修改"); b3.addActionListener( new UpdateAction()); JButton b4 = new JButton("查询"); b4.addActionListener( new QueryAction()); JButton b5 = new JButton("导出"); b5.addActionListener(new OutputAction()); JButton b6 = new JButton("导入"); b6.addActionListener(new InputAction()); JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout()); p1.add(b1); p1.add(b2); p1.add(b3); p1.add(b4); p1.add(b5); p1.add(b6); cp.add(p1,BorderLayout.SOUTH); JLabel label = new JLabel("学员管理"); cp.add(label,BorderLayout.NORTH); name.addElement("学号"); name.addElement("姓名"); name.addElement("固定电话"); name.addElement("手机"); name.addElement( "家庭住址"); name.addElement("证件号码"); defaultModel = new DefaultTableModel(cs.showTable(), name){ private static final long serialVersionUID = 1L; // 设置Jtable单元格不可修改 public boolean isCellEditable(int row,int column){ return false; } }; table = new JTable(defaultModel); table.setPreferredScrollableViewportSize(new Dimension(400, 80)); JScrollPane s = new JScrollPane(table); cp.add(s,BorderLayout.CENTER); JMenuBar menubar = new JMenuBar(); JMenu menu = new JMenu("学员管理"); JMenu menu1 = new JMenu("信息查询"); JMenu menu2 = new JMenu("数据备份"); JMenuItem addData = new JMenuItem("添加"); addData.addActionListener(new AdddataAction()); JMenuItem delData = new JMenuItem("删除"); delData.addActionListener(new DeleteAction()); JMenuItem upData = new JMenuItem("修改"); upData.addActionListener( new UpdateAction()); JMenuItem queryData = new JMenuItem("查询"); queryData.addActionListener(new QueryAction()); JMenuItem input = new JMenuItem("导入"); input.addActionListener(new InputAction()); JMenuItem output = new JMenuItem("导出"); output.addActionListener(new OutputAction()); menu.add(addData); menu.add(delData); menu.add(upData); menu1.add(queryData); menu2.add(input); menu2.add(output); menubar.add(menu); menubar.add(menu1); menubar.add(menu2); final JPopupMenu popup = new JPopupMenu(); JMenuItem addData1 = new JMenuItem("添加"); addData1.addActionListener(new AdddataAction()); JMenuItem delData1 = new JMenuItem("删除"); delData1.addActionListener(new DeleteAction()); JMenuItem upData1 = new JMenuItem("修改"); upData1.addActionListener( new UpdateAction()); JMenuItem queryData1 = new JMenuItem("查询"); queryData1.addActionListener(new QueryAction()); JMenuItem input1 = new JMenuItem("导入"); JMenuItem output1 = new JMenuItem("导出"); output1.addActionListener(new OutputAction()); popup.add(addData1); popup.add(delData1); popup.add(upData1); popup.add(queryData1); popup.add(input1); popup.add(output1); //设置table鼠标右键单击菜单事件 table.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent e) { if(e.isPopupTrigger()) popup.show(e.getComponent(),e.getX(),e.getY()); } public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) popup.show(e.getComponent(), e.getX(), e.getY()); } }); cp.add(menubar,BorderLayout.NORTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addWindowListener (new WindowAdapter(){ //设置窗口关闭事件 public void windowClosing(WindowEvent e) { cs.shutDown(); } }); table.addMouseListener(new UpdateAction()); setResizable(false); setVisible(true); } //添加学员信息监听器 class AdddataAction implements ActionListener { public void actionPerformed(ActionEvent e) { final AppendFrame append = new AppendFrame(); append.b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String str =append.text1.getText(); if(!str.matches("\\d++")) { JOptionPane.showMessageDialog(null, "学号只能为数字,请重新输入!"); } else { int id = Integer.parseInt(str); String name = append.text2.getText(); String phone = append.text3.getText(); if(!phone.matches("\\d{8}")) JOptionPane.showMessageDialog(null, "电话号码只能为8位数字,请重新输入!"); else { String mobile = append.text4.getText(); if(!mobile.matches("\\d{11}")) JOptionPane.showMessageDialog(null, "手机号码只能为11位数字,请重新输入!"); else { String address = append.text5.getText(); String cert = append.text6.getText(); if(!cert.matches("\\d{18}")) JOptionPane.showMessageDialog(null, "身份证号码为18位数字,请重新输入!"); else { cs.addData(id, name, phone, mobile, address, cert); append.text1.setText(""); append.text2.setText(""); append.text3.setText(""); append.text4.setText(""); append.text5.setText(""); append.text6.setText(""); JOptionPane.showMessageDialog(null, "添加信息成功!"); // append.dispose(); refresh(); } } } } } }); } } //删除信息监听器 class DeleteAction implements ActionListener { public void actionPerformed(ActionEvent e) { int row = table.getSelectedRow(); if(row == -1){ JOptionPane.showMessageDialog(null, "请选择你要删除的数据!"); } else { int id = (Integer)table.getValueAt(row, 0); cs.deleteData(id); refresh(); } } } //修改信息监听器 class UpdateAction extends MouseAdapter implements ActionListener { public void actionDisplay() //内部类updateAction的方法 { int row = table.getSelectedRow(); if(row==-1) { JOptionPane.showMessageDialog(null, "请选择你要修改的数据!"); } else{ final AppendFrame af = new AppendFrame(); int id = (Integer)table.getValueAt(row, 0); String str = Integer.toString(id); String name = table.getValueAt(row, 1).toString(); String phone = table.getValueAt(row, 2).toString(); String mobile = table.getValueAt(row, 3).toString(); String address = table.getValueAt(row, 4).toString(); String cert = table.getValueAt(row, 5).toString(); af.text1.setText(str); af.text1.setEditable(false); af.text2.setText(name); af.text3.setText(phone); af.text4.setText(mobile); af.text5.setText(address); af.text6.setText(cert); //AppendFrame中(button)b1的监听器 af.b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String s = af.text1.getText(); int id = Integer.parseInt(s); String name = af.text2.getText(); String phone = af.text3.getText(); if(!phone.matches("\\d{8}")) JOptionPane.showMessageDialog(null, "电话号码只能为8位数字,请重新输入!"); else { String mobile = af.text4.getText(); if(!mobile.matches("\\d{11}")) JOptionPane.showMessageDialog(null, "手机号码只能为11位数字,请重新输入!"); { String address = af.text5.getText(); String cert = af.text6.getText(); if(!cert.matches("\\d{18}")) JOptionPane.showMessageDialog(null, "身份证号码为18位数字,请重新输入!"); else { cs.upData(id,name, phone, mobile, address, cert); JOptionPane.showMessageDialog(null, "修改数据成功!"); af.dispose(); refresh(); } } } } }); } } //鼠标双击事件 public void mouseClicked(MouseEvent e) { if(e.getClickCount()==2) { actionDisplay(); } } //修改信息监听器 public void actionPerformed(ActionEvent e) { actionDisplay(); } } //查询信息监听器 class QueryAction implements ActionListener { public void actionPerformed(ActionEvent e) { final QueryFrame query = new QueryFrame(); query.button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int row = query.table.getSelectedRow(); if(row==-1) { JOptionPane.showMessageDialog(null, "请选择你要修改的数据!"); } else{ final AppendFrame af = new AppendFrame(); int id = (Integer) query.table.getValueAt(row, 0); String str = Integer.toString(id); String name = query.table.getValueAt(row, 1).toString(); String phone = query.table.getValueAt(row, 2).toString(); String mobile = query.table.getValueAt(row, 3).toString(); String address = query.table.getValueAt(row, 4).toString(); String cert = query.table.getValueAt(row, 5).toString(); af.text1.setText(str); af.text1.setEditable(false); af.text2.setText(name); af.text3.setText(phone); af.text4.setText(mobile); af.text5.setText(address); af.text6.setText(cert); // AppendFrame中b1(button)的监听器 af.b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String s = af.text1.getText(); int id = Integer.parseInt(s); String name = af.text2.getText(); String phone = af.text3.getText(); String mobile = af.text4.getText(); String address = af.text5.getText(); String cert = af.text6.getText(); cs.upData(id,name, phone, mobile, address, cert); JOptionPane.showMessageDialog(null, "修改数据成功!"); af.dispose(); refresh(); query.refresh(id); } }); } } }); //QueryFrame中button2(删除)的监听器 query.button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { int row = query.table.getSelectedRow(); if(row == -1){ JOptionPane.showMessageDialog(null, "请选择你要删除的数据!"); } else { int id = (Integer)query.table.getValueAt(row, 0); cs.deleteData(id); int rowcount = query.defaultModel.getRowCount() - 1; if (rowcount>= 0) { query.defaultModel.removeRow(row); query.defaultModel.setRowCount(rowcount); } query.refresh(id); refresh(); } } }); } } //导出数据库信息监听器 class OutputAction implements ActionListener { public void actionPerformed(ActionEvent e) { cs.Output(); JOptionPane.showMessageDialog(null, "导出数据成功!"); } } //导入数据库信息监听器 class InputAction implements ActionListener { public void actionPerformed(ActionEvent e) { try{ BufferedReader br = new BufferedReader( new FileReader("f:/data/file.txt")); int result = JOptionPane.showConfirmDialog(table,"是否清空数据库?","警告",JOptionPane.YES_NO_OPTION); if(result == JOptionPane.YES_OPTION) cs.deleteData(); String ss; while((ss = br.readLine())!= null) { String [] str = ss.split(","); int id = Integer.parseInt(str[0]); String name = str[1].trim(); String phone = str[2].trim(); String mobile = str[3].trim(); String address = str[4].trim(); String cert = str[5].trim(); cs.addData(id, name, phone, mobile, address, cert); } refresh(); } catch(IOException ioe){ ioe.printStackTrace(); } } } //JTable的刷新 public void refresh() { table.setModel(new DefaultTableModel(cs.showTable(),name) { private static final long serialVersionUID = 1L; public boolean isCellEditable(int row,int column){ return false; } } ); table.updateUI(); } } 3.AppendFrame类 import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; /* * 添加、修改信息窗口 */ public class AppendFrame extends JFrame { private static final long serialVersionUID = 1L; JTextField text1 = new JTextField(28); JTextField text2 = new JTextField(28); JTextField text3 = new JTextField(28); JTextField text4 = new JTextField(28); JTextField text5 = new JTextField(28); JTextField text6 = new JTextField(28); JButton b1 = new JButton("确定"); public AppendFrame() { setTitle("编辑学员信息"); setLocation(300, 250); setSize(400, 300); setLayout(new BorderLayout()); Container cp = getContentPane(); JButton b2 = new JButton("取消"); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { dispose(); } }); JPanel p1 = new JPanel(); p1.setLayout(new FlowLayout()); p1.add(b1); p1.add(b2); cp.add(p1,BorderLayout.SOUTH); JLabel label = new JLabel("基本信息"); cp.add(label,BorderLayout.NORTH); JPanel p2 = new JPanel(); p2.setLayout(new FlowLayout()); JLabel label1 = new JLabel("学 号:"); JLabel label2 = new JLabel("姓 名:"); JLabel label3 = new JLabel("固定电话:"); JLabel label4 = new JLabel("手 机:"); JLabel label5 = new JLabel("家庭住址:"); JLabel label6 = new JLabel("证件号码:"); p2.add(label1); p2.add(text1); p2.add(label2); p2.add(text2); p2.add(label3); p2.add(text3); p2.add(label4); p2.add(text4); p2.add(label5); p2.add(text5); p2.add(label6); p2.add(text6); cp.add(p2,BorderLayout.CENTER); setResizable(false); setVisible(true); } //重写dispose()方法 public void dispose() { super.dispose(); } } 4.QueryFrame类 import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.table.DefaultTableModel; /* * 查询数据信息窗口 */ public class QueryFrame extends JFrame { private static final long serialVersionUID = 1L; ConnServer cs = new ConnServer(); Vector<String> name = new Vector<String>(); Vector v = new Vector(); JTable table = new JTable(); DefaultTableModel defaultModel; Container cp = getContentPane(); JButton button1 = new JButton("修改"); JButton button2 = new JButton("删除"); JRadioButton rb1 = new JRadioButton("学号",false); JRadioButton rb2 = new JRadioButton("姓名",false); ButtonGroup group = new ButtonGroup(); JTextField text = new JTextField(12); public QueryFrame() { setTitle("学生信息查询"); setBounds(200, 200, 500, 400); JPanel p1 = new JPanel(); JPanel p2 = new JPanel(); JButton button = new JButton("查询"); group.add(rb1); group.add(rb2); p1.add(rb1); p1.add(rb2); p1.add(text); p1.add(button); p2.add(button1); p2.add(button2); cp.add(p1,BorderLayout.NORTH); cp.add(p2,BorderLayout.SOUTH); name.addElement("学号"); name.addElement("姓名"); name.addElement("固定电话"); name.addElement("手机"); name.addElement( "家庭住址"); name.addElement("证件号码"); //主窗口监听器 addWindowListener(new WindowAdapter(){ public void windowClosed(WindowEvent e) { dispose(); } }); defaultModel = new DefaultTableModel(v, name){ private static final long serialVersionUID = 1L; //设置JTable 单元格不可编辑 public boolean isCellEditable(int row,int column){ return false; } }; table = new JTable(defaultModel); table.setPreferredScrollableViewportSize(new Dimension(400, 80)); JScrollPane s = new JScrollPane(table); cp.add(s,BorderLayout.CENTER); //添加button监听器 button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { if(rb1.isSelected()) { try{ String s = text.getText(); if(!s.matches("\\d++")) JOptionPane.showMessageDialog(null, "学号只能是数字,请重新输入查询!"); else { int id = Integer.parseInt(s); int i = 0; Statement stmt = cs.conn.createStatement(); String ss = "select * from student where id ="+id+""; ResultSet rs = stmt.executeQuery(ss); while(rs.next()) { i = rs.getInt("id"); } if(!(i==id)) JOptionPane.showMessageDialog(null, "无此学生信息!"); else { v= cs.showTable(id); refresh(id); } } }catch (SQLException sql){ sql.printStackTrace(); } } else { if(rb2.isSelected()) { String name = text.getText(); v = cs.showTable(name); refresh(name); } else { JOptionPane.showMessageDialog(null, "请选择查询条件!"); } } } }); setVisible(true); } //刷新JTable方法 public void refresh(int id) { table.setModel(new DefaultTableModel(cs.showTable(id),name) { private static final long serialVersionUID = 1L; public boolean isCellEditable(int row,int column){ return false; } } ); table.updateUI(); } //重载refresh()方法 public void refresh(String cname) { table.setModel(new DefaultTableModel(cs.showTable(cname),name) { private static final long serialVersionUID = 1L; public boolean isCellEditable(int row,int column){ return false; } } ); table.updateUI(); } //重写dispose(),隐藏窗口 public void dispose() { super.dispose(); } } 5.ConnServer数据库操作类 import java.io.BufferedWriter; import java.io.FileWriter; 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.Vector; import javax.swing.JOptionPane; /* * 数据库连接及相关操作 */ public class ConnServer { Connection conn = null; Statement stmt = null; ResultSet rs = null; public ConnServer() { //数据库的连接 try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo?useUnicode=true&characterEncoding=gb2312","root","root"); } catch (ClassNotFoundException e) { System.err.println("装载 JDBC 驱动程序失败!"); e.printStackTrace(); } catch (SQLException e) { System.out.println("无法连接数据库!"); e.printStackTrace(); } } //在JTable中显示数据库中数据的方法 public Vector showTable() { Vector<Object> data = new Vector<Object>(); try{ Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from student"); while(rs.next()) { Vector<Object> rowData = new Vector<Object>(); rowData.addElement(rs.getInt("id")); rowData.addElement(rs.getString("name")); rowData.addElement(rs.getString("phone")); rowData.addElement(rs.getString("mobile")); rowData.addElement(rs.getString("address")); rowData.addElement(rs.getString("cert")); data.addElement(rowData); } } catch(SQLException e){ e.printStackTrace(); } return data; } //重载 showTable() 方法 public Vector showTable(int id) { Vector<Object> data = new Vector<Object>(); try{ Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from student where id ="+id+""); while(rs.next()) { Vector<Object> rowData = new Vector<Object>(); rowData.addElement(rs.getInt("id")); rowData.addElement(rs.getString("name")); rowData.addElement(rs.getString("phone")); rowData.addElement(rs.getString("mobile")); rowData.addElement(rs.getString("address")); rowData.addElement(rs.getString("cert")); data.addElement(rowData); } } catch(SQLException e){ e.printStackTrace(); } return data; } // 重载 showTable() 方法 public Vector showTable(String name) { Vector<Object> data = new Vector<Object>(); try{ Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from student where name like'%"+name+"%'"); while(rs.next()) { Vector<Object> rowData = new Vector<Object>(); rowData.addElement(rs.getInt("id")); rowData.addElement(rs.getString("name")); rowData.addElement(rs.getString("phone")); rowData.addElement(rs.getString("mobile")); rowData.addElement(rs.getString("address")); rowData.addElement(rs.getString("cert")); data.addElement(rowData); } } catch(SQLException e){ e.printStackTrace(); } return data; } //删除数据库中的数据 public void deleteData() { try{ stmt = conn.createStatement(); stmt.execute("delete from student"); //JOptionPane.showMessageDialog(null, "清空数据成功!"); } catch(SQLException e){ e.printStackTrace(); } } //重载deleteData()方法 public void deleteData(int id) { try{ stmt = conn.createStatement(); stmt.execute("delete from student where id ="+id); JOptionPane.showMessageDialog(null, "删除数据成功!"); } catch(SQLException e){ e.printStackTrace(); } } // 在数据库中添加数据的方法 public void addData(int id,String name,String phone,String mobile,String address,String cert) { int i = 0; try{ stmt = conn.createStatement(); String ss = "select * from student where id ="+id+""; rs = stmt.executeQuery(ss); while(rs.next()) { i = rs.getInt("id"); } if(i==id) { JOptionPane.showMessageDialog(null, "你输入的学号重复,请重新输入!"); } else{ String str="insert into student values ('"+id+"','"+name+"','"+phone+"','"+mobile+"','"+address+"','"+cert+"')"; stmt.execute(str); //JOptionPane.showMessageDialog(null, "添加信息成功!"); } } catch (SQLException e){ e.printStackTrace(); } } //修改数据库中数据的方法 public void upData(int id,String name,String phone,String mobile,String address,String cert) { try{ stmt = conn.createStatement(); String str = "update student SET name = '"+name+"',phone = '"+phone+"',mobile = '"+mobile+"',address = '"+address+"',cert = '"+cert+"'where id="+id+""; stmt.execute(str); } catch(SQLException e){ e.printStackTrace(); } } //导出数据的方法 public void Output() { try{ BufferedWriter bw = new BufferedWriter(new FileWriter("f:/data/file.txt")); Vector data = showTable(); for(int i=0;i<data.size();i++) { Vector v = (Vector)data.get(i); String id = Integer.toString((Integer) v.get(0)); String name = (String)v.get(1); String phone = (String)v.get(2); String mobile = (String)v.get(3); String address = (String)v.get(4); String cert = (String)v.get(5); bw.write(id.trim()+","+"\t"+name.trim()+","+"\t"+phone.trim()+","+"\t"+mobile.trim()+","+"\t"+address.trim()+","+"\t"+cert.trim()); bw.newLine(); } bw.flush(); bw.close(); } catch (IOException e){ e.printStackTrace(); } } //关闭数据库连接的方法 public void shutDown() { try{ if(stmt != null) stmt.close(); stmt = null; if(rs != null) rs.close(); rs = null; if(conn != null) conn.close(); conn = null; } catch(SQLException e){ e.printStackTrace(); } } } 项目中用到了MySql数据库,再把MySql的驱动添加到项目中就可以了. 至于数据库方面我就不罗嗦了,只有一张表student,字段:id,name,phone,mobile,address,cert