您好,登錄后才能下訂單哦!
這篇文章主要介紹Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng)的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
本文為大家分享了Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng),供大家參考,具體內(nèi)容如下
因?yàn)閷?shí)驗(yàn)室要交作業(yè)然后就做了一個(gè)學(xué)生管理系統(tǒng),貼個(gè)代碼紀(jì)念一下,做的太急界面什么的也比較差。
還有一些小細(xì)節(jié)沒(méi)有完善不過(guò)還是能實(shí)現(xiàn)主要的功能的。
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("學(xué)生管理系統(tǒng)") ; //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("學(xué)號(hào):"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性別:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("學(xué)院:"); 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("插入數(shù)據(jù)成功!"); }catch (SQLException b){ b.printStackTrace(); }finally{ try{ conn.close(); System.out.println("MySQL 關(guān)閉成功"); }catch (SQLException c){ System.out.println("MySQL 關(guān)閉失敗 "); c.printStackTrace(); } } }} ); buttonreturn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ Window window = new Window(); } }); this.setTitle("添加學(xué)生信息"); 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("學(xué)號(hào):"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性別:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("學(xué)院:"); 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("查詢學(xué)生信息"); 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("學(xué)號(hào):"); JLabel jlname = new JLabel("姓名:"); JLabel jlsex = new JLabel("性別:"); JLabel jlbirthday = new JLabel("出生日期:"); JLabel jldepartment = new JLabel("學(xué)院:"); 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("修改學(xué)生信息"); 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("學(xué)號(hào):"); 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("刪除學(xué)生信息"); 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("學(xué)號(hào)"); columnNames.add("姓名"); columnNames.add("性別"); columnNames.add("出生日期"); columnNames.add("學(xué)院"); 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("瀏覽學(xué)生信息"); this.setLayout(new GridLayout(2,5)); this.add(jpforbutton); this.setLocation(300,300); this.setSize(500,300); this.setVisible(true); this.setResizable(false); } }
一些運(yùn)行的界面:
以上是“Java基于MySQL實(shí)現(xiàn)學(xué)生管理系統(tǒng)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。