重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍Java基于MySQL实现学生管理系统的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联建站主营沙坪坝网站建设的网络公司,主营网站建设方案,手机APP定制开发,沙坪坝h5微信平台小程序开发搭建,沙坪坝网站营销推广欢迎沙坪坝等地区企业咨询
本文为大家分享了Java基于MySQL实现学生管理系统,供大家参考,具体内容如下
因为实验室要交作业然后就做了一个学生管理系统,贴个代码纪念一下,做的太急界面什么的也比较差。
还有一些小细节没有完善不过还是能实现主要的功能的。
Window是主界面
package First; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Window { public static void main(String[] args){ JFrame jframe = new JFrame("学生管理系统") ; //window Dimension d = new Dimension(400,300); Point p = new Point (250,350); jframe.setSize(d); jframe.setLocation(p); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jframe.setVisible(true); JButton button1 = new JButton("添加"); JButton button2 = new JButton("修改"); JButton button3 = new JButton("查询"); JButton button4 = new JButton("删除"); JButton button5 = new JButton("浏览"); FlowLayout flow = new FlowLayout(FlowLayout.LEFT,10,10); JPanel panel = new JPanel(flow); panel.add(button1); panel.add(button2); panel.add(button3); panel.add(button4); panel.add(button5); jframe.add(panel); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Add add = new Add(); } }); button2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Change change = new Change(); } }); button3.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Ask ask = new Ask(); } }); button4.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Delete delete = new Delete(); } }); button5.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Look look = new Look(); } }); } }
Add是添加
package First; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.mysql.jdbc.Driver; import First.Window; public class Add extends JFrame { private static final long serialVersionUID = -1928970409928880648L; JLabel jlnumber = new JLabel("学号:"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性别:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("学院:"); JTextField jtnumber = new JTextField("",20); JTextField jtname = new JTextField("",20); JTextField jtsex = new JTextField("",20); JTextField jtbirthday = new JTextField("",20); JTextField jtdepartment = new JTextField("",20); JButton buttonadd = new JButton("添加"); JButton buttonreturn = new JButton("返回"); public Add() { JPanel jpnumber = new JPanel(); JPanel jpname = new JPanel(); JPanel jpsex = new JPanel(); JPanel jpbirthday = new JPanel(); JPanel jpdepartment = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonadd); jpforbutton.add(buttonreturn); buttonadd.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Add Connection conn = null; Statement stat = null; PreparedStatement ps=null; String sql = "INSERT INTO student(number,name,sex,birthday,department) " + "values(?,?,?,?,?)"; try{ Class.forName("Driver"); System.out.println("JBDC 加载成功!"); }catch(Exception a){ System.out.println("JBDC 狗带!"); a.printStackTrace(); } try{ conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123"); ps=conn.prepareStatement(sql); ps.setString(1,jtnumber.getText()); ps.setString(2,jtname.getText()); ps.setString(3,jtsex.getText()); ps.setString(4,jtbirthday.getText()); ps.setString(5,jtdepartment.getText()); ps.executeUpdate(); //System.out.println("MySQL 连接成功!"); //stat = conn.createStatement(); //stat.executeUpdate(sql); //System.out.println("插入数据成功!"); }catch (SQLException b){ b.printStackTrace(); }finally{ try{ conn.close(); System.out.println("MySQL 关闭成功"); }catch (SQLException c){ System.out.println("MySQL 关闭失败 "); c.printStackTrace(); } } }} ); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Window window = new Window(); } }); this.setTitle("添加学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Ask是查询
package First; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import First.Window; public class Ask extends JFrame { private static final long serialVersionUID = -1928970409928880648L; JLabel jlnumber = new JLabel("学号:"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性别:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("学院:"); JTextField jtnumber = new JTextField("",20); JLabel jname = new JLabel(); JLabel jsex = new JLabel(); JLabel jbirthday = new JLabel(); JLabel jdepartment = new JLabel(); JButton buttonask = new JButton("查询"); JButton buttonreturn = new JButton("返回"); public Ask() { JPanel jpnumber = new JPanel(); JPanel jpname = new JPanel(); JPanel jpsex = new JPanel(); JPanel jpbirthday = new JPanel(); JPanel jpdepartment = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jname); jpsex.add(jlsex); jpsex.add(jsex); jpbirthday.add(jlbirthday); jpbirthday.add(jbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jdepartment); jpforbutton.add(buttonask); jpforbutton.add(buttonreturn); buttonask.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Connection conn = null; ResultSet res = null; Statement stat = null; String sql = "SELECT number,name,sex,birthday,department FROM student;"; try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception d){ System.out.println("jdbc fall"); d.printStackTrace(); } try{ conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123"); stat=conn.createStatement(); res=stat.executeQuery(sql); while (res.next()) { if (res.getString(1).equals(jtnumber.getText())) { jname.setText(res.getString(2)); jsex.setText(res.getString(3)); jbirthday.setText(res.getString(4)); jdepartment.setText(res.getString(5)); break; } } }catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally{ try{ conn.close(); }catch(SQLException ar){ ar.printStackTrace(); } }}} ); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Window window = new Window(); } }); this.setTitle("查询学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Change是修改
package First; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import First.Window; public class Change extends JFrame { private static final long serialVersionUID = -1928970409928880648L; JLabel jlnumber = new JLabel("学号:"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性别:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("学院:"); JTextField jtnumber = new JTextField("",20); JTextField jtname = new JTextField("",20); JTextField jtsex = new JTextField("",20); JTextField jtbirthday = new JTextField("",20); JTextField jtdepartment = new JTextField("",20); JButton buttonchange = new JButton("修改"); JButton buttonreturn = new JButton("返回"); public Change() { JPanel jpnumber = new JPanel(); JPanel jpname = new JPanel(); JPanel jpsex = new JPanel(); JPanel jpbirthday = new JPanel(); JPanel jpdepartment = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpname.add(jlname); jpname.add(jtname); jpsex.add(jlsex); jpsex.add(jtsex); jpbirthday.add(jlbirthday); jpbirthday.add(jtbirthday); jpdepartment.add(jldepartment); jpdepartment.add(jtdepartment); jpforbutton.add(buttonchange); jpforbutton.add(buttonreturn); buttonchange.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String number = jtnumber.getText(); String name = jtname.getText(); String sex = jtsex.getText(); String birthday = jtbirthday.getText(); String department = jtdepartment.getText(); Connection conn = null; ResultSet res = null; Statement stat = null; String sql = "SELECT number,name,sex,birthday,department FROM student;"; try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception d){ System.out.println("jdbc fall"); d.printStackTrace(); } try{ conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123"); stat=conn.createStatement(); res=stat.executeQuery(sql); while (res.next()) { //change if (res.getString(1).equals(jtnumber.getText())) { try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception d){ System.out.println("jdbc fall"); d.printStackTrace(); } String sql2="UPDATE student SET name='"+name+"' WHERE number='"+jtnumber.getText()+"'"; String sql3="UPDATE student SET sex='"+sex+"' WHERE number='"+jtnumber.getText()+"'"; String sql4="UPDATE student SET birthday='"+birthday+"' WHERE number='"+jtnumber.getText()+"'"; String sql5="UPDATE student SET department='"+department+"' WHERE number='"+jtnumber.getText()+"'"; try { conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123"); stat=conn.createStatement(); stat.executeUpdate(sql2); stat.executeUpdate(sql3); stat.executeUpdate(sql4); stat.executeUpdate(sql5); } catch (SQLException g) { // TODO Auto-generated catch block g.printStackTrace(); }try{ stat.close(); conn.close(); }catch(SQLException ar){ ar.printStackTrace(); } break; } //change end } }catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } finally{ try{ conn.close(); }catch(SQLException ar){ ar.printStackTrace(); } } } }); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Window window = new Window(); } }); this.setTitle("修改学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpname); this.add(jpsex); this.add(jpbirthday); this.add(jpdepartment); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Delete是删除
package First; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import First.Window; public class Delete extends JFrame { private static final long serialVersionUID = -1928970409928880648L; JLabel jlnumber = new JLabel("学号:"); JTextField jtnumber = new JTextField("",20); JButton buttondelete = new JButton("删除"); JButton buttonreturn = new JButton("返回"); public Delete() { JPanel jpnumber = new JPanel(); JPanel jpforbutton = new JPanel(new GridLayout(1,1)); jpnumber.add(jlnumber); jpnumber.add(jtnumber); jpforbutton.add(buttondelete); jpforbutton.add(buttonreturn); buttondelete.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String number = jtnumber.getText(); Connection conn = null; ResultSet res = null; Statement stat = null; String sql = "DELETE FROM student WHERE number='"+number+"'"; try{ Class.forName("com.mysql.jdbc.Driver"); }catch(Exception a){ a.printStackTrace(); } try{ conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123"); stat = conn.createStatement(); stat.executeUpdate(sql); }catch(SQLException h){ h.printStackTrace(); }finally{ try{ conn.close(); System.out.println("close success!"); }catch(SQLException j){ System.out.println("close go die!"); j.printStackTrace(); } } } }); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Window window = new Window(); } }); this.setTitle("删除学生信息"); this.setLayout(new GridLayout(9,1)); this.add(jpnumber); this.add(jpforbutton); this.setLocation(400,300); this.setSize(350,300); this.setVisible(true); } }
Look是浏览
package First; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import First.Window; public class Look extends JFrame { private static final long serialVersionUID = -1928970409928880648L; Connection conn = null; PreparedStatement ps = null; ResultSet res = null; //JButton buttonlook = new JButton("浏览"); //JButton buttonreturn = new JButton("返回"); JTable jtable; JScrollPane jscrollpane = new JScrollPane(); Vector columnNames = null; Vector rowData = null; public Look() { JPanel jpforbutton = new JPanel(new GridLayout(1,1)); columnNames = new Vector(); columnNames.add("学号"); columnNames.add("姓名"); columnNames.add("性别"); columnNames.add("出生日期"); columnNames.add("学院"); rowData = new Vector(); //jpforbutton.add(buttonlook); //jpforbutton.add(buttonreturn); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaStu","root","123"); ps = conn.prepareStatement("SELECT * FROM student"); res = ps.executeQuery(); while (res.next()) { Vector hang = new Vector(); hang.add(res.getString(1)); hang.add(res.getString(2)); hang.add(res.getString(3)); hang.add(res.getString(4)); hang.add(res.getString(5)); rowData.add(hang); } System.out.println("load ok!"); }catch (Exception q){ q.printStackTrace(); System.out.println("go die"); }finally{ try{ res.close(); ps.close(); conn.close(); System.out.println("close ok"); }catch (SQLException o){ o.printStackTrace(); System.out.println("go die 2"); } } jtable = new JTable(rowData,columnNames); jscrollpane = new JScrollPane(jtable); this.add(jscrollpane); this.setTitle("浏览学生信息"); this.setLayout(new GridLayout(2,5)); this.add(jpforbutton); this.setLocation(300,300); this.setSize(500,300); this.setVisible(true); this.setResizable(false); } }
一些运行的界面:
以上是“Java基于MySQL实现学生管理系统的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!