由於實驗室要交做業而後就作了一個學生管理系統 貼個代碼記念一下~
作的太急界面什麼的也比較差
還有一些小細節沒有完善不過仍是能實現主要的功能的~

Window是主界面
html
- 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();
- }
- });
-
- }
-
- }
複製代碼java
Add是添加
mysql
- 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);
-
- }
-
-
- }
-
複製代碼sql
Ask是查詢
this
- 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);
-
-
- }
-
-
- }
-
複製代碼orm
Change是修改
htm
- 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);
-
-
- }
-
-
- }
-
複製代碼blog
Delete是刪除
get
- 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);
-
-
- }
-
-
-
- }
-
-
-
複製代碼it
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);
-
- }
-
-
- }
複製代碼
一些運行的界面~


