学生成绩管理系统的设计-实践周作业
目錄
- 介紹
- 文件結(jié)構(gòu)
- 代碼
- Main.java
- UserView.java
- jdbcConnection
- check包
- DBexecute
- Mainfunction包
- CheckAccount
- CheckAccount1
- InsertStudentInformation
- InsertTeacherInformation
- view包
- Student_Coures
- Teacher_Checkscore
- Teacher_Information
- 下載鏈接--程序的完整包
- 數(shù)據(jù)庫表格的設(shè)計
- 系統(tǒng)功能和實現(xiàn)
- 部分頁面截圖
- 詳細(xì)實驗報告介紹
- 心得
介紹
該程序是鏈接mysql數(shù)據(jù)庫的,然后我們是使用了阿里云的云服務(wù)器配置了數(shù)據(jù)庫(這個是另一個同學(xué)做的),不過一下代碼對于本地數(shù)據(jù)庫應(yīng)該也是可以實現(xiàn)的,然后實現(xiàn)了一些基本的功能比如增刪查改,使用了javaswing來寫頁面(比較丑哈哈),學(xué)生和老師注冊登錄啊之類的功能。
文件結(jié)構(gòu)
代碼
Main.java
import Mainfunction.CheckAccount; import Mainfunction.CheckAccount2; import Mainfunction.InsertStudentInformation; import Mainfunction.InsertTeacherInformation; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; class Fun implements ActionListener{Connection connection;JFrame frame=new JFrame();JLabel lb1;//頂部需要的組件Button bt1,bt2,bt3,bt4;//定義中間所需要的組件// 中部有4個panel,選項卡窗口管理JTabbedPane pane;JPanel panel_s,panel_t;JLabel label1,label2;JLabel label3,label4;JLabel stu;//用來提示這是學(xué)生的登錄界面,學(xué)生默認(rèn)輸入長度為11位JLabel tea;//用來提示這是老師的登錄界面,老師默認(rèn)輸入長度為3位JTextField textField_student;//學(xué)生區(qū)域的學(xué)生輸入框JPasswordField Password_student;//密碼輸入框JTextField textField_teacher;//老師區(qū)域輸入框JPasswordField Password_teacher;//老師區(qū)域密碼輸入框//底部所需要的組件JPanel jPanel1;JButton jButton;//3學(xué)生注冊界面相關(guān)控件JPanel stu_register;JLabel S_register_lb1,S_register_lb2;JTextField S_register;JPasswordField S_pass;JLabel Student;//4老師注冊界面相關(guān)控件JPanel tea_register;JLabel T_register_lb1,T_register_lb2;JTextField T_register;JPasswordField T_pass;JLabel Teacher;@Overridepublic void actionPerformed(ActionEvent e) {}public Fun(){//數(shù)據(jù)庫連接jdbcConnection jdbc=new jdbcConnection();connection=jdbc.giveConnection();//處理底部lb1=new JLabel(new ImageIcon("src/mao.png"));jPanel1=new JPanel();//jButton=new JButton("確定");//jPanel1.add(jButton);//將button組件加進去//后面舍棄了這種做法直接四個界面加入button應(yīng)對不同的情況//處理中部panel_s=new JPanel(new GridLayout(3,3));label1=new JLabel("學(xué)生賬號",JLabel.CENTER);label2=new JLabel("學(xué)生密碼",JLabel.CENTER);textField_student=new JTextField();Password_student=new JPasswordField();stu=new JLabel("這里是學(xué)生登錄界面",JLabel.CENTER);bt1=new Button("login");//按控件順序加入到每個板面panel_s.add(label1);panel_s.add(textField_student);panel_s.add(label2);panel_s.add(Password_student);panel_s.add(stu);panel_s.add(bt1);//老師panel_t=new JPanel(new GridLayout(3,3));label3=new JLabel("老師賬號",JLabel.CENTER);label4=new JLabel("老師密碼",JLabel.CENTER);textField_teacher=new JTextField();Password_teacher=new JPasswordField();bt2=new Button("login");tea=new JLabel("這里是老師登錄界面",JLabel.CENTER);panel_t.add(label3);panel_t.add(textField_teacher);panel_t.add(label4);panel_t.add(Password_teacher);panel_t.add(tea);panel_t.add(bt2);//學(xué)生注冊stu_register=new JPanel(new GridLayout(3,3));S_register_lb1=new JLabel("注冊學(xué)生賬號",JLabel.CENTER);S_register_lb2=new JLabel("注冊學(xué)生密碼",JLabel.CENTER);S_register=new JTextField();S_pass=new JPasswordField();Student=new JLabel("這里是學(xué)生注冊",JLabel.CENTER);bt3=new Button("register");Student.setForeground(Color.RED);stu_register.add(S_register_lb1);stu_register.add(S_register);stu_register.add(S_register_lb2);stu_register.add(S_pass);stu_register.add(Student);stu_register.add(bt3);//老師注冊tea_register=new JPanel(new GridLayout(3,3));T_register_lb1=new JLabel("注冊老師賬號",JLabel.CENTER);T_register_lb2=new JLabel("注冊老師密碼",JLabel.CENTER);T_register=new JTextField();T_pass=new JPasswordField();Teacher=new JLabel("這里是進行老師注冊",JLabel.CENTER);bt4=new Button("register");Teacher.setForeground(Color.BLUE);tea_register.add(T_register_lb1);tea_register.add(T_register);tea_register.add(T_register_lb2);tea_register.add(T_pass);tea_register.add(Teacher);tea_register.add(bt4);//創(chuàng)建選項卡窗口//加入頁面pane=new JTabbedPane();pane.add("學(xué)生登錄",panel_s);pane.add("老師登錄",panel_t);pane.add("學(xué)生注冊",stu_register);pane.add("老師注冊",tea_register);frame.setSize(550,340);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setLocationRelativeTo(null);frame.setVisible(true);frame.add(lb1,"North");frame.add(jPanel1,"South");frame.add(pane,"Center");bt1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String account= textField_student.getText();String password=new String(Password_student.getPassword());CheckAccount checkAccount=new CheckAccount(frame,connection,account,password);if(checkAccount.check()){UserView view= new UserView(1,connection,account);frame.dispose();// view.number=1;}else{JOptionPane.showMessageDialog(frame,"登錄錯誤","Error",JOptionPane.INFORMATION_MESSAGE);}}});bt2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {String account= textField_teacher.getText();String password=new String(Password_teacher.getPassword());CheckAccount2 checkAccount=new CheckAccount2(frame,connection,account,password);if(checkAccount.check()){UserView view= new UserView(2,connection,account);frame.dispose();// view.number=1;}else{JOptionPane.showMessageDialog(frame,"登錄錯誤","Error",JOptionPane.INFORMATION_MESSAGE);}}});bt3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try{//讀取賬號和密碼String account= S_register.getText();String password=new String(S_pass.getPassword());//進行數(shù)字的判斷int number=Integer.parseInt(account);//進行數(shù)字長度的判斷if(account.length()==6){InsertStudentInformation s=new InsertStudentInformation(frame,connection,account,password);if(s.giveBool()){JOptionPane.showMessageDialog(frame,"注冊成功","Yes",JOptionPane.INFORMATION_MESSAGE);}}else{JOptionPane.showMessageDialog(frame,"賬號不合法,注意為6位數(shù)字","Error",JOptionPane.INFORMATION_MESSAGE);}}catch (Exception exception){JOptionPane.showMessageDialog(frame,"賬號不合法,注意為三位數(shù)字","Error",JOptionPane.INFORMATION_MESSAGE);}//將學(xué)生的數(shù)據(jù)存入數(shù)據(jù)庫}});bt4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try{//讀取賬號和密碼String account= T_register.getText();String password=new String(T_pass.getPassword());//進行數(shù)字的判斷int number=Integer.parseInt(account);//進行數(shù)字長度的判斷if(account.length()==3){InsertTeacherInformation t=new InsertTeacherInformation(frame,connection,account,password);//返回bool值檢查是否已存在已有賬號if(t.giveBool()){JOptionPane.showMessageDialog(frame,"注冊成功","Yes",JOptionPane.INFORMATION_MESSAGE);}}else{JOptionPane.showMessageDialog(frame,"賬號不合法,注意為3位數(shù)字","Error",JOptionPane.INFORMATION_MESSAGE);}}catch (Exception exception){JOptionPane.showMessageDialog(frame,"賬號不合法,注意為三位數(shù)字","Error",JOptionPane.INFORMATION_MESSAGE);}//老師的數(shù)據(jù)存入數(shù)據(jù)庫}});} } public class Main {public static void main(String[] args) {Fun Fun=new Fun();} }UserView.java
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.Connection; import check.*; import check.Teacher_InsertScore; import view.*;import javax.swing.*;public class UserView {//用戶idprivate String id;//數(shù)據(jù)庫private Connection connection;private JFrame userView = new JFrame("界面");private JPanel panel = new JPanel(new FlowLayout());public int number;//添加功能選擇private JButton studentInformation = new JButton("學(xué)生信息");private JButton Student_Course = new JButton("學(xué)生選課");private JButton Teacher_Information = new JButton("老師信息");private JButton Teacher_CheckScore = new JButton("查看分?jǐn)?shù)");private JButton Teacher_InsertScore = new JButton("登記分?jǐn)?shù)");private JButton function6 = new JButton("數(shù)據(jù)庫備份");//先聲明功能的選擇,到以后寫好包之后即可刪除private JFrame fuction5_View;private JFrame fuction6_View;public UserView(int number, Connection connection, String id) {this.connection = connection;this.number = number;this.id = id;init();}public void init() {if (this.number == 1) {studentInformation.setFont(new Font("", Font.BOLD, 15));panel.add(studentInformation);Student_Course.setFont(new Font("", Font.BOLD, 15));panel.add(Student_Course);} else {Teacher_Information.setFont(new Font("", Font.BOLD, 15));panel.add(Teacher_Information);Teacher_InsertScore.setFont(new Font("", Font.BOLD, 15));panel.add(Teacher_InsertScore);Teacher_CheckScore.setFont(new Font("", Font.BOLD, 15));panel.add(Teacher_CheckScore);function6.setFont(new Font("", Font.BOLD, 15));panel.add(function6);}//添加佛大圖片JLabel Fosu = new JLabel(new ImageIcon("src/img.png"));panel.add(Fosu);Fosu.setBounds(0, 150, 700, 200);//添加文字JLabel showName = new JLabel("成績管理系統(tǒng)");showName.setFont(new Font("", Font.BOLD, 22));panel.add(showName);userView.add(panel);userView.setSize(500, 500);userView.setLocationRelativeTo(null);userView.setVisible(true);userView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/** 給按鈕添加事件監(jiān)聽器*///學(xué)生信息頁面studentInformation.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {userView.setVisible(false);Student_Information student_information = new Student_Information(connection, id, userView);student_information.studentView.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {userView.setVisible(true);userView.setLocationRelativeTo(null);super.windowClosing(e);}});}});//學(xué)生選課頁面Student_Course.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//這里為功能輸入,后期直接寫好包之后導(dǎo)入即可userView.setVisible(false);Student_Course course1 = new Student_Course(connection, id);course1.course.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {userView.setVisible(true);userView.setLocationRelativeTo(null);super.windowClosing(e);}});}});//老師信息頁面Teacher_Information.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {userView.setVisible(false);Teacher_Information course1 = new Teacher_Information(connection, id, userView);course1.teacherView.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {userView.setVisible(true);userView.setLocationRelativeTo(null);super.windowClosing(e);}});}});//登記分?jǐn)?shù)。Teacher_InsertScore.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {userView.setVisible(false);Teacher_InsertScore teacher_insertScore=new Teacher_InsertScore(id);teacher_insertScore.jFrame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {userView.setVisible(true);userView.setLocationRelativeTo(null);super.windowClosing(e);}});}});//檢查分?jǐn)?shù)Teacher_CheckScore.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {userView.setVisible(false);Teacher_CheckScore teacher_checkScore = new Teacher_CheckScore(connection, id);teacher_checkScore.jf.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {userView.setVisible(true);userView.setLocationRelativeTo(null);super.windowClosing(e);}});}});//功能六頁面function6.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//這里為功能輸入,后期直接寫好包之后導(dǎo)入即可 // fuction2_View = Singleton2.getInstance(); 單例模式fuction6_View = new JFrame();fuction6_View.setTitle("第二個窗口");fuction6_View.setSize(500, 500);fuction6_View.setLocationRelativeTo(null);userView.setVisible(false);fuction6_View.setVisible(true);fuction6_View.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {userView.setVisible(true);super.windowClosing(e);}});}});} }jdbcConnection
import java.sql.*; public class jdbcConnection {Connection connection;public jdbcConnection() {Connection conn = null;try {try {DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());} catch (SQLException e) {e.printStackTrace();}String url = "jdbc:mysql://XXXXX";//這里為自己的數(shù)據(jù)庫的鏈接String user = "XXXX"; //這里的信息是需要更改的賬號String password = "XXXX";//這里的信息是需要更改的密碼conn = DriverManager.getConnection(url, user, password);System.out.println("數(shù)據(jù)庫連接對象" + conn);connection= conn;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public Connection giveConnection(){return connection;} }check包
DBexecute
package check; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBexecute {Connection connection;private PreparedStatement pstm;private ResultSet rs;public Connection getConnection() throws Exception {try {String url = "jdbc:mysql://120.25.152.50:3306/database";String user = "root"; //這里的信息是需要更改的String password = "zqy939597A";connection = DriverManager.getConnection(url, user, password);System.out.println("數(shù)據(jù)庫連接對象" + connection);return connection;} catch (Exception e) {throw new SQLException("驅(qū)動錯誤或連接失敗!");}}//查詢操作public ResultSet executeQurey(String sql,String a) {try {//得到preparaStatement語句pstm = connection.prepareStatement(sql);if(a!=null){pstm.setString(1,a);}//執(zhí)行sql語句rs = pstm.executeQuery();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}//返回查詢結(jié)果集return rs;}//插入、刪除操作public int executeUpdate(String sql, String[] data,String course) {int count = 0;try {pstm = connection.prepareStatement(sql);if (data != null) {for (int i = 0; i < data.length; i++) {pstm.setString(i + 1, data[i]);}count = pstm.executeUpdate();String sql1 = "update score set course=? where id=?";pstm = connection.prepareStatement(sql1);System.out.println("!!!"+course);System.out.println("!!!"+data[0]);pstm.setString(1,course);pstm.setString(2,data[0]);pstm.executeUpdate();}} catch (Exception e) {e.printStackTrace();}//返回修改成功的行數(shù)return count;}public int executeDelete(String sql, String[] data) {int count = 0;try {pstm = connection.prepareStatement(sql);if (data != null) {for (int i = 0; i < data.length; i++) {pstm.setString(i + 1, data[i]);}count = pstm.executeUpdate();}} catch (Exception e) {e.printStackTrace();}//返回修改成功的行數(shù)return count;}public void closeAll() {// 如果rs不空,關(guān)閉rsif (rs != null) {try {rs.close();} catch (SQLException e) {e.printStackTrace();}}// 如果pstm不空,關(guān)閉pstmif (pstm != null) {try {pstm.close();} catch (SQLException e) {e.printStackTrace();}}// 如果conn不空,關(guān)閉connif (connection != null) {try {connection.close();} catch (SQLException e) {e.printStackTrace();}}} }Teacher_InsertScore
package check;import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.Vector;import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel;public class Teacher_InsertScore {private String id;public JFrame jFrame =new JFrame();//滾動面板private JScrollPane sptable;//表格private JTable table;//放置按鈕的面板private JPanel panel;//設(shè)置按鈕private JButton btsave, btdelete, btadd, btflush;//設(shè)置默認(rèn)表格格式private DefaultTableModel model;private String course;Connection connection;public Teacher_InsertScore(String id) {this.id=id;returnCourse();jFrame.setTitle(course+"課程成績管理頁面");//設(shè)置表格model = new DefaultTableModel();table = new JTable(model);table.setForeground(Color.BLACK); // 字體顏色table.setFont(new Font(null, Font.PLAIN, 14)); // 字體樣式table.setSelectionForeground(Color.DARK_GRAY); // 選中后字體顏色table.setSelectionBackground(Color.LIGHT_GRAY); // 選中后字體背景table.setGridColor(Color.GRAY);table.getTableHeader().setFont(new Font("楷體", Font.BOLD, 14)); // 設(shè)置表頭名稱字體樣式table.getTableHeader().setForeground(Color.RED); // 設(shè)置表頭名稱字體顏色table.getTableHeader().setResizingAllowed(false); // 設(shè)置不允許手動改變列寬table.getTableHeader().setReorderingAllowed(false); // 設(shè)置不允許拖動重新排序各列//將表格放入滾動面板中sptable = new JScrollPane(table);//將滾動面板放入主面板中部jFrame.add(sptable, BorderLayout.CENTER);btsave = new JButton("保存");btadd = new JButton("添加");btdelete = new JButton("刪除");btflush = new JButton("刷新");panel = new JPanel();panel.add(btadd);panel.add(btsave);panel.add(btdelete);panel.add(btflush);btsave.setVisible(false);jFrame.add(panel, BorderLayout.SOUTH);//顯示初始數(shù)據(jù)showData();//設(shè)置界面基本數(shù)據(jù)jFrame.setSize(1000, 600);jFrame.setLocationRelativeTo(null);jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);jFrame.setVisible(true);//添加監(jiān)聽器,因為每個實現(xiàn)的功能不同,所以這里使用匿名內(nèi)部類來實現(xiàn)btadd.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {addData();}});btsave.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubsaveDate();}});btdelete.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubdeleteDate();}});btflush.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubshowData();}});}void showData() {// TODO Auto-generated method stubtry {returnCourse();String sql = "select id,name,score from score where course= ? ";DBexecute db = new DBexecute();connection=db.getConnection();//連接數(shù)據(jù)庫db.getConnection();//獲取返回集ResultSet rs = db.executeQurey(sql,course);//獲取列名System.out.println(id);System.out.println(course);ResultSetMetaData rsmt = rs.getMetaData();//獲取列數(shù)int count = rsmt.getColumnCount();//創(chuàng)建一個Vector集合存放列名titleVector<String> title = new Vector<>();//存放列名for (int i = 1; i <= count; i++) {title.add(rsmt.getColumnLabel(i));}//存放集合(行數(shù)據(jù))的集合Vector<Vector<String>> data = new Vector();//判斷表中有無數(shù)據(jù)int rowCount = 0;//將數(shù)據(jù)放入data中while (rs.next()) {rowCount++;//設(shè)置一個集合存放行數(shù)據(jù),在放入data中Vector<String> rowData = new Vector<>();for (int i = 1; i <= count; i++) {rowData.add(rs.getString(i));}data.add(rowData);}if (rowCount == 0) {//若行為0即數(shù)據(jù)庫表中沒有數(shù)據(jù),便將title列名放入table即可model.setDataVector(null, title);} else {//若有數(shù)據(jù),則將調(diào)用setDataVector將title插入data的第0列model.setDataVector(data, title);}} catch (Exception e) {e.printStackTrace();JOptionPane.showMessageDialog(jFrame, "系統(tǒng)錯誤!請仔細(xì)檢查!");}}private void addData() {// TODO Auto-generated method stubint rowCount = model.getRowCount();int rowCount1 = table.getRowCount();//最好使用Object[]model.insertRow(rowCount, new String[]{"0", "0","0"});btadd.setVisible(false);btsave.setVisible(true);}private void saveDate() {// TODO Auto-generated method stubint rowCount = table.getRowCount() - 1;//獲取自己填寫的數(shù)據(jù)String id = table.getValueAt(rowCount, 0).toString();String name = table.getValueAt(rowCount, 1).toString();String score = table.getValueAt(rowCount, 2).toString();DBexecute db = new DBexecute();try {db.getConnection();String sql = "insert score values(?,?,?,?)";int count = db.executeUpdate(sql, new String[]{id,name,null,score},course);showData();btadd.setVisible(true);btsave.setVisible(false);if (count == 1) {JOptionPane.showMessageDialog(jFrame, "插入數(shù)據(jù)成功!");} else {JOptionPane.showMessageDialog(jFrame, "插入數(shù)據(jù)失敗!");}} catch (Exception e) {e.printStackTrace();} finally {db.closeAll();}}private void deleteDate() {// TODO Auto-generated method stubint index[] = table.getSelectedRows();if (index == null) {JOptionPane.showMessageDialog(jFrame, "請選擇需要刪除的數(shù)據(jù)!", "刪除", JOptionPane.PLAIN_MESSAGE);} else {try {int k = JOptionPane.showConfirmDialog(jFrame, "是否要刪除這條記錄?", "刪除", JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);if (k == JOptionPane.YES_OPTION) {DBexecute db = new DBexecute();try {db.getConnection();String sql = "delete from score where id=?";String name = table.getValueAt(index[0], 0).toString();int count = db.executeDelete(sql, new String[]{name});showData();if (count == 1) {JOptionPane.showMessageDialog(jFrame, "刪除數(shù)據(jù)成功!", "成功", JOptionPane.INFORMATION_MESSAGE);} else {JOptionPane.showMessageDialog(jFrame, "刪除數(shù)據(jù)失敗!", "失敗", JOptionPane.WARNING_MESSAGE);}} catch (Exception e) {e.printStackTrace();} finally {db.closeAll();}}} catch (Exception ee) {JOptionPane.showMessageDialog(jFrame, "抱歉!刪除數(shù)據(jù)失敗!【系統(tǒng)異常!】", "失敗:", 0);}}}public void returnCourse() {try {DBexecute db = new DBexecute();db.getConnection();String sql="select course from 老師基本信息表 where teacherid=?";ResultSet rs=db.executeQurey(sql,id);if(rs.next()){this.course=rs.getString(1);}} catch (Exception e) {}} }Mainfunction包
CheckAccount
package Mainfunction;import javax.swing.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet;public class CheckAccount {private Connection connection;private String account;private JFrame frame;private String password;PreparedStatement stmt=null;ResultSet result=null;public CheckAccount(JFrame frame,Connection connection, String account, String password){this.frame=frame;this.account=account;this.password=password;this.connection=connection;}public boolean check(){try {String sql = "select * from studentregister where username= ? and password= ? ";stmt = connection.prepareStatement(sql);stmt.setString(1, account);stmt.setString(2, password);result = stmt.executeQuery();if (result.next()) {JOptionPane.showMessageDialog(frame,"登錄成功","Yes",JOptionPane.INFORMATION_MESSAGE);return true;}else{return false;}}catch (Exception e){return false;}} }CheckAccount1
package Mainfunction;import javax.swing.*; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet;public class CheckAccount2 {private Connection connection;private String account;private JFrame frame;private String password;PreparedStatement stmt = null;ResultSet result = null;public CheckAccount2(JFrame frame, Connection connection, String account, String password) {this.frame = frame;this.account = account;this.password = password;this.connection = connection;}public boolean check() {try {String sql = "select * from teacherregister where username= ? and password= ? ";stmt = connection.prepareStatement(sql);stmt.setString(1, account);stmt.setString(2, password);result = stmt.executeQuery();if (result.next()) {JOptionPane.showMessageDialog(frame,"登錄成功","Yes",JOptionPane.INFORMATION_MESSAGE);return true;} else {return false;}} catch (Exception e) {return false;}} }InsertStudentInformation
package Mainfunction;import javax.swing.*; import java.sql.*;public class InsertStudentInformation {private String account;private String password;private Connection connection;private JFrame frame;PreparedStatement stmt=null;ResultSet result=null;public InsertStudentInformation(JFrame frame,Connection connection,String account,String password){this.account=account;this.password=password;this.connection=connection;this.frame=frame;}public boolean giveBool(){try {String sql = "select * from studentregister where username= ?";stmt = connection.prepareStatement(sql);stmt.setString(1, account);result = stmt.executeQuery();//查詢到已有賬號就是已經(jīng)存在,則返回falseif (result.next()) {JOptionPane.showMessageDialog(frame,"注冊錯誤,已存在此賬號","No",JOptionPane.INFORMATION_MESSAGE);return false;}//插入注冊的賬號和密碼String sql1 = "insert into studentregister value (?,?)";String sql2 = "insert into 學(xué)生基本信息表 value (?,?,?,?)";PreparedStatement pstmt = connection.prepareStatement(sql1);PreparedStatement pstmt2 = connection.prepareStatement(sql2);pstmt.setString(1,account);pstmt.setString(2,password);pstmt2.setString(1,account);pstmt2.setString(2,"null");pstmt2.setString(3,"null");pstmt2.setString(4,"null");int count1 = pstmt.executeUpdate();int count2 = pstmt2.executeUpdate();if(count1 ==1&&count2==1){return true;}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;}return true;} }InsertTeacherInformation
``
package Mainfunction;
import javax.swing.;
import java.sql.;
public class InsertTeacherInformation {
private String account;
private String password;
private Connection connection;
private JFrame frame;
PreparedStatement stmt=null;
ResultSet result=null;
public InsertTeacherInformation(JFrame frame,Connection connection,String account,String password){
this.account=account;
this.password=password;
this.connection=connection;
this.frame=frame;
view包
Student_Coures
package view;import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.sql.Connection; import java.sql.PreparedStatement;public class Student_Course extends Component implements ActionListener {ActionEvent event;private Connection connection;private String id;private int selectclass;public JFrame course=new JFrame();private JPanel panel = new JPanel(new GridLayout());public Student_Course(Connection connection,String id){this.connection=connection;this.id=id;init();}void init(){course=new JFrame();course.setTitle("學(xué)生選課查詢");course.setSize(500,500);JRadioButton b1=new JRadioButton("大數(shù)據(jù)技術(shù)");b1.setFont(new Font("", Font.BOLD, 15));JRadioButton b2=new JRadioButton("算法分析與設(shè)計");b2.setFont(new Font("", Font.BOLD, 15));JRadioButton b3=new JRadioButton("Kotlin語言");b3.setFont(new Font("", Font.BOLD, 15));JRadioButton b4=new JRadioButton("匯編語言");b4.setFont(new Font("", Font.BOLD, 15));b1.setBounds(75,50,100,30);b2.setBounds(75,90,100,30);b3.setBounds(75,130,100,30);b4.setBounds(75,170,100,30);ButtonGroup bg=new ButtonGroup();JTextArea area=new JTextArea("請選擇你的選課,點擊選擇后頁面會自動退出,請注意",40,40);area.setFont(new Font("", Font.BOLD, 22));area.setEditable(false);area.setLineWrap(true);bg.add(b1);bg.add(b2);bg.add(b3);bg.add(b4);panel.add(area);panel.add(b1);panel.add(b2);panel.add(b3);panel.add(b4);course.add(panel);course.setLocationRelativeTo(null);course.setVisible(true);b1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(b1.isSelected()){JOptionPane.showMessageDialog(course,"你已選擇大數(shù)據(jù)技術(shù)課程成功");selectclass=1;updateCourse(selectclass);course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );}}});b2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(b2.isSelected()){JOptionPane.showMessageDialog(course,"你已選擇算法設(shè)計與分析課程成功");selectclass=2;updateCourse(selectclass);course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );}}});b3.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(b3.isSelected()){JOptionPane.showMessageDialog(course,"你已選擇Kotlin語言課程成功");selectclass=3;updateCourse(selectclass);course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );}}});b4.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {if(b4.isSelected()){JOptionPane.showMessageDialog(course,"你已選擇匯編語言成功");selectclass=4;updateCourse(selectclass);course.dispatchEvent(new WindowEvent(course,WindowEvent.WINDOW_CLOSING) );}}});}@Overridepublic void actionPerformed(ActionEvent e) {}public void updateCourse(int i){String a="大數(shù)據(jù)技術(shù)";String b="算法分析與設(shè)計";String c="Kotlin語言";String d="匯編語言";switch (i){case 1:try{String sql = "update 學(xué)生基本信息表 set course=? where id=?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1,a);pstmt.setString(2,id);pstmt.executeUpdate();}catch (Exception abc){}break;case 2:try{String sql = "update 學(xué)生基本信息表 set course=? where id=?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1,b);pstmt.setString(2,id);pstmt.executeUpdate();}catch (Exception abc){}break;case 3:try{String sql = "update 學(xué)生基本信息表 set course=? where id=?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1,c);pstmt.setString(2,id);pstmt.executeUpdate();}catch (Exception abc){}break;case 4:try{String sql = "update 學(xué)生基本信息表 set course=? where id=?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1,d);pstmt.setString(2,id);pstmt.executeUpdate();}catch (Exception abc){}break;}} } ### Student_Informationpackage view;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Student_Information {
ResultSet result=null;
private String id;
private String name;
private String classes;
private String course;
private JFrame userview;
private Connection connection;
private int score;
public JFrame studentView=new JFrame();
private JPanel panel = new JPanel(new GridLayout());
JTextField textField_stu=new JTextField();
JTextField textField_class=new JTextField();
JTextField textField_account=new JTextField();
JTextField textField_course=new JTextField();
JTextField fun_course=new JTextField();
JButton submit=new JButton(“提交”);
JButton Return=new JButton(“返回”);
public Student_Information(Connection connection,String id,JFrame userview){
this.connection=connection;
this.userview=userview;
this.id=id;
init();
}
void init(){
studentView=new JFrame();
studentView.setTitle(“學(xué)生信息查詢”);
studentView.setSize(500,500);
studentView.setLocationRelativeTo(null);
studentView.setVisible(true);
GridLayout layout=new GridLayout(6,2);
JPanel panel=new JPanel(layout);//設(shè)置為layout布局
JLabel L_stu=new JLabel(“名字”);
JLabel L_class=new JLabel(“班級”);
JLabel L_account=new JLabel(“賬戶”);
JLabel L_course=new JLabel(“已選課程”);
JLabel L_fun=new JLabel(“所選課程成績”);
L_fun.setFont(new Font(“”,Font.BOLD,22));
}
Teacher_Checkscore
package view;import com.mysql.cj.protocol.Resultset;import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.concurrent.ExecutionException;//大概每個模塊的粗略模板 public class Teacher_CheckScore {private Connection connection;private String course;private String id;private int fun;public JFrame jf;ResultSet resultset = null;public Teacher_CheckScore(Connection connection, String id) {this.connection = connection;this.id = id;exe();}void exe() {try {jf = new JFrame("查看成績");jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);returnCourse();JPanel panel = new JPanel(new BorderLayout());String lSqlStr = "select count(*) from score where course=?";PreparedStatement statement1 = connection.prepareStatement(lSqlStr);statement1.setString(1, course);resultset = statement1.executeQuery();int count = 0;if (resultset.next()) {count = resultset.getInt(1);System.out.println("numberOfRows= " + count);String sql = " select * from score where course=? ";PreparedStatement statement = connection.prepareStatement(sql);statement.setString(1, course);resultset = statement.executeQuery();System.out.println(count);Object[][] rowData = new Object[count][4];System.out.println(rowData.length);int i = 0;while (resultset.next()) {rowData[i][0] = resultset.getString(1);rowData[i][1] = resultset.getString(2);rowData[i][2] = resultset.getString(3);rowData[i][3] = resultset.getString(4);i++;}// 表頭(列名)Object[] columnNames = {"id", "名字", "科目", "分?jǐn)?shù)"};// 表格所有行數(shù)據(jù)// 創(chuàng)建一個表格,指定 所有行數(shù)據(jù) 和 表頭JTable table = new JTable(rowData, columnNames);// 設(shè)置表格內(nèi)容顏色table.setForeground(Color.BLACK); // 字體顏色table.setFont(new Font(null, Font.PLAIN, 14)); // 字體樣式table.setSelectionForeground(Color.DARK_GRAY); // 選中后字體顏色table.setSelectionBackground(Color.LIGHT_GRAY); // 選中后字體背景table.setGridColor(Color.GRAY); // 網(wǎng)格顏色// 設(shè)置表頭table.getTableHeader().setFont(new Font(null, Font.BOLD, 14)); // 設(shè)置表頭名稱字體樣式table.getTableHeader().setForeground(Color.RED); // 設(shè)置表頭名稱字體顏色table.getTableHeader().setResizingAllowed(false); // 設(shè)置不允許手動改變列寬table.getTableHeader().setReorderingAllowed(false); // 設(shè)置不允許拖動重新排序各列table.setEnabled(false);// 設(shè)置行高table.setRowHeight(30);// 第一列列寬設(shè)置為40table.getColumnModel().getColumn(0).setPreferredWidth(40);// 設(shè)置滾動面板視口大小(超過該大小的行數(shù)據(jù),需要拖動滾動條才能看到)table.setPreferredScrollableViewportSize(new Dimension(500, 500));// 把 表格 放到 滾動面板 中(表頭將自動添加到滾動面板頂部)JScrollPane scrollPane = new JScrollPane(table);// 添加 滾動面板 到 內(nèi)容面板panel.add(scrollPane);JPanel panel1=new JPanel();JButton seekScore = new JButton("查看成績");seekScore.addActionListener(new AbstractAction() {@Overridepublic void actionPerformed(ActionEvent e) {giveFun();JOptionPane.showMessageDialog(jf,"該課程的平均分為"+fun,"avg",JOptionPane.INFORMATION_MESSAGE);}});panel1.add(seekScore);// 設(shè)置 內(nèi)容面板 到 窗口jf.setContentPane(panel);jf.add(panel1, BorderLayout.SOUTH);jf.pack();jf.setLocationRelativeTo(null);jf.setVisible(true);}} catch (Exception e) {}}public void giveFun(){try{String sql = " select avg(score) from score where course=? ";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1, course);ResultSet resultset = pstmt.executeQuery();if (resultset.next())fun = resultset.getInt(1);}catch (Exception e){}}public void returnCourse() {try {String sql = " select course from 老師基本信息表 where teacherid=? ";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1, id);ResultSet resultset = pstmt.executeQuery();if (resultset.next())course = resultset.getString(1);else {JOptionPane.showMessageDialog(jf,"該老師沒有選擇教任何的課","No",JOptionPane.INFORMATION_MESSAGE);jf.dispose();}} catch (Exception e) {}}public void function2() {}public void function3() {}public void function4() {}}Teacher_Information
package view;import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;public class Teacher_Information {ResultSet result = null;private String id;private String name;private String pos;private String course;private JFrame userview;private Connection connection;public JFrame teacherView = new JFrame();private JPanel panel = new JPanel(new GridLayout());JTextField textField_tea = new JTextField();JTextField textField_pos = new JTextField();JTextField textField_account = new JTextField();JButton submit = new JButton("提交");JButton Return = new JButton("返回");JComboBox<String> comboBox;int coursenumber;public Teacher_Information(Connection connection, String id, JFrame userview) {this.connection = connection;this.userview = userview;this.id = id;init();}void init() {teacherView = new JFrame();teacherView.setTitle("老師信息查詢");teacherView.setSize(500, 500);teacherView.setLocationRelativeTo(null);teacherView.setVisible(true);GridLayout layout = new GridLayout(5, 2);JPanel panel = new JPanel(layout);//設(shè)置為layout布局JLabel L_tea = new JLabel("姓名");JLabel L_pos = new JLabel("職位");JLabel L_account = new JLabel("賬戶");JLabel L_choose = new JLabel("講授的課程");textField_account.setEditable(false);L_tea.setFont(new Font("", Font.BOLD, 22));L_pos.setFont(new Font("", Font.BOLD, 22));L_account.setFont(new Font("", Font.BOLD, 22));L_choose.setFont(new Font("", Font.BOLD, 12));String[] listData = {"無", "大數(shù)據(jù)技術(shù)", "算法分析與設(shè)計", "Kotlin語言", "匯編語言"};comboBox = new JComboBox<String>(listData);course=returnCourse();System.out.println(course);System.out.println(coursenumber);coursenumber=returnNumber(course);comboBox.setSelectedIndex(coursenumber);comboBox.addItemListener(new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {String lesson = (String) comboBox.getSelectedItem();try {String sql = "update 老師基本信息表 set course=? where teacherid=?";PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1, lesson);pstmt.setString(2, id);pstmt.executeUpdate();int number=returnNumber(lesson);coursenumber=number;} catch (SQLException ex) {}}});textField_pos.setFont(new Font("", Font.BOLD, 22));textField_tea.setFont(new Font("", Font.BOLD, 22));textField_account.setFont(new Font("", Font.BOLD, 22));submit.setFont(new Font("", Font.BOLD, 22));Return.setFont(new Font("", Font.BOLD, 22));panel.add(L_account);panel.add(textField_account);panel.add(L_pos);panel.add(textField_pos);panel.add(L_tea);panel.add(textField_tea);panel.add(L_choose);panel.add(comboBox);panel.add(submit);panel.add(Return);teacherView.add(panel);teacherView.setVisible(true);getInformation();function();}public void getInformation() {try {PreparedStatement stmt = null;String sql = "select * from 老師基本信息表 where teacherid=?";stmt = connection.prepareStatement(sql);stmt.setString(1, id);result = stmt.executeQuery();System.out.println(result);if (result.next()) {textField_account.setText(result.getString(1));textField_tea.setText(result.getString(3));textField_pos.setText(result.getString(2));}} catch (Exception eee) {}}public int returnNumber(String course){if(course.equals("匯編語言"))return 4;else if(course.equals("大數(shù)據(jù)技術(shù)"))return 1;else if(course.equals("算法分析與設(shè)計"))return 2;else if(course.equals("Kotlin語言"))return 3;elsereturn 0;}public String returnCourse(){try{String sql = "select course from 老師基本信息表 where teacherid = ?";System.out.println("1");PreparedStatement stmt1= connection.prepareStatement(sql);System.out.println("1");stmt1.setString(1, id);System.out.println(id);ResultSet resultSet= stmt1.executeQuery();if(resultSet.next()){return resultSet.getString(1);}return "無";}catch (Exception e){return "無";}}public void function() {submit.addActionListener(new AbstractAction() {@Overridepublic void actionPerformed(ActionEvent e) {try {id = textField_account.getText();name = textField_tea.getText();pos = textField_pos.getText();String sql = "update 老師基本信息表 set name=?,position=? where teacherid=?";//PreparedStatement pstmt = connection.prepareStatement(sql);pstmt.setString(1, name);pstmt.setString(2, pos);pstmt.setString(3, id);int count = pstmt.executeUpdate();if (count == 1) {JOptionPane.showMessageDialog(teacherView, "提交成功", "Yes", JOptionPane.INFORMATION_MESSAGE);}} catch (Exception ee) {}}});Return.addActionListener(new AbstractAction() {@Overridepublic void actionPerformed(ActionEvent e) {userview.setVisible(true);teacherView.dispose();}});}}下載鏈接–程序的完整包
鏈接: https://download.csdn.net/download/qq_21315871/85839034
數(shù)據(jù)庫表格的設(shè)計
ER圖
Score表
studentregister
teacherregister
課程與成績關(guān)系表
老師基本信息表
學(xué)生基本信息表
系統(tǒng)功能和實現(xiàn)
由于該程序的一切都是在連接數(shù)據(jù)庫的基礎(chǔ)上實現(xiàn)的,所以我們組使用了阿里云的云服務(wù)器進行配置mysql服務(wù)器。(服務(wù)器的配置不是本人做就不過多介紹),同時java運行環(huán)境啊,服務(wù)器的運行環(huán)境啊,jdbc包這些就這里就不過多贅述了。
部分頁面截圖
詳細(xì)實驗報告介紹
鏈接: https://download.csdn.net/download/qq_21315871/85839115
心得
該程序還是比較爛的,此博文只是來記錄一下自己實驗周做過的事情哈哈哈。自己編寫的時候也借鑒了很多資料和很多內(nèi)容。
總結(jié)
以上是生活随笔為你收集整理的学生成绩管理系统的设计-实践周作业的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 分布式日志收集ELK
- 下一篇: android 获取定位坐标,在百度地图