Eclipse+Java+Swing實現學生成績管理系統的實例代碼
一、系統介紹
本系統實現瞭用戶登錄,實現瞭對學生成績的增刪改查,實現瞭用戶修改密碼功能,采用MD5加密算法,數據庫使用Mysql8.0.13,界面良好。
二、系統展示
1.登錄頁
2.主頁面
3.查詢學生成績
4.修改學生成績
5.添加學生成績
6.修改系統密碼
三、系統實現 Student.java
package com.sjsq.model; import java.util.ArrayList; import java.util.List; /** * 學生實體類 * @author shuijianshiqing * */ public class Student { // 學號 private String sId; // 姓名 private String sName; // 性別 private String sSex; // 學院 private String sCollege; private String sC; private String sMath; private String sEnglish; private String sChinese; private String sJava; public Student() { super(); } public Student(String sId, String sName, String sSex, String sCollege, String sC, String sMath, String sEnglish, String sChinese, String sJava) { super(); this.sId = sId; this.sName = sName; this.sSex = sSex; this.sCollege = sCollege; this.sC = sC; this.sMath = sMath; this.sEnglish = sEnglish; this.sChinese = sChinese; this.sJava = sJava; } public static List<Student> students=new ArrayList<Student>(); public String getsId() { return sId; } public void setsId(String sId) { this.sId = sId; } public String getsName() { return sName; } public void setsName(String sName) { this.sName = sName; } public String getsSex() { return sSex; } public void setsSex(String sSex) { this.sSex = sSex; } public String getsCollege() { return sCollege; } public void setsCollege(String sCollege) { this.sCollege = sCollege; } public String getsC() { return sC; } public void setsC(String sC) { this.sC = sC; } public String getsMath() { return sMath; } public void setsMath(String sMath) { this.sMath = sMath; } public String getsEnglish() { return sEnglish; } public void setsEnglish(String sEnglish) { this.sEnglish = sEnglish; } public String getsChinese() { return sChinese; } public void setsChinese(String sChinese) { this.sChinese = sChinese; } public String getsJava() { return sJava; } public void setsJava(String sJava) { this.sJava = sJava; } @Override public String toString() { return "Student [sId=" + sId + ", sName=" + sName + ", sSex=" + sSex + ", sCollege=" + sCollege + ", sC=" + sC + ", sMath=" + sMath + ", sEnglish=" + sEnglish + ", sChinese=" + sChinese + ", sJava=" + sJava + "]"; } }
StudentDao.java
package com.sjsq.dao; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.sjsq.model.Student; import com.sjsq.util.DBUtil; public class StudentDao { public static int progress; // private final String FILE_PATH="d:\\student.xls"; public static StudentDao getInstance() { StudentDao studentDao; return studentDao = new StudentDao(); } // 查詢所有學生的信息 public ResultSet queryAll() { ResultSet rs = null; String sql = "select * from Student"; System.out.println("------查詢所有學生信息------"); System.out.println("sql語句:" + sql); DBUtil db = new DBUtil(); try { rs = db.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 查詢所有學生信息,並且返回List public List<Student> queryAllStudent() { List<Student> listStudent = new ArrayList<Student>(); String sql = "select * from student"; System.out.println("------查詢所有學生信息------"); System.out.println("sql語句:" + sql); DBUtil db = new DBUtil(); ResultSet rs = null; try { rs = db.executeQuery(sql); while (rs.next()) { Student studentTmp = new Student(); studentTmp.setsId(rs.getString("sId")); studentTmp.setsName(rs.getString("sName")); studentTmp.setsSex(rs.getString("sSex")); studentTmp.setsCollege(rs.getString("sCollege")); studentTmp.setsC(rs.getString("sC")); studentTmp.setsMath(rs.getString("sMath")); studentTmp.setsEnglish(rs.getString("sEnglish")); studentTmp.setsChinese(rs.getString("sChinese")); studentTmp.setsJava(rs.getString("sJava")); listStudent.add(studentTmp); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } } catch (SQLException e) { e.printStackTrace(); } db.close(); } return listStudent; } public static int getprogress() { return progress; } // 按照條件查詢 public ResultSet queryByCondition(String[] data) { ResultSet rs = null; String sql = "select * from student where 1=1"; int i = 0; // 學號 if (!(data[0].equals(""))) { sql += " and sid='" + data[0] + "' "; } // 姓名 if (!(data[1].equals(""))) { sql += " and sname='" + data[1] + "' "; } // 省份 if (!(data[2].equals(""))) { sql += " and senglish='" + data[2] + "' "; } // 性別 if (!(data[3].equals(""))) { sql += " and ssex='" + data[3] + "' "; } System.out.println("------查詢所有學生信息------"); System.out.println("sql語句:" + sql); DBUtil db = new DBUtil(); try { rs = db.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } // 插入學生信息,並且返回插入結果 public boolean insertStudentInformation(Student student) { DBUtil DBUtil = new DBUtil(); // 通過student對象設置sid的值 String sid = student.getsId(); String sname = student.getsName(); String ssex = student.getsSex(); String scollege = student.getsCollege(); String sc = student.getsC(); String smath = student.getsMath(); String senglish = student.getsEnglish(); String schinese = student.getsChinese(); String sjava = student.getsJava(); String sql = "insert into student(sid,sname,ssex,scollege,sc,smath,senglish,schinese,sjava)" + "values('" + sid + "','" + sname + "','" + ssex + "','" + scollege+ "','" + sc+ "','" + smath + "','" + senglish + "','" + schinese + "','" + sjava + "')"; System.out.println("------插入學生信息------"); System.out.println("sql語句:" + sql); // 向數據庫插入數據語句 // 定義一個boolean型變量,用於判斷插入數據是否成功 boolean flag = false; try { // 更新數據庫信息 flag = DBUtil.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } DBUtil.close(); // 返回執行結果 return flag; } // 統計同一學號有多少人 public int queryForsidinformation(String sid) { DBUtil db = new DBUtil(); // 通過sid查詢數據庫中是否有相同sid數據 String sql = "select * from student where sid=" + sid; System.out.println("------驗證學生學號信息------"); System.out.println("sql語句:" + sql); ResultSet rs = null; int count = 0; try { // 更新數據庫保存到結果集裡 rs = (ResultSet) db.executeQuery(sql); // 找到相同的count++ if (rs.next()) { count++; } } catch (SQLException e) { e.printStackTrace(); } // 關閉數據庫連接 db.close(); // 返回count return count; } // 更新學生信息,這裡學號不能更新 public boolean updateStudentInformation(Student student) { DBUtil DBUtil = new DBUtil(); // scollege,sc,smath,senglish,schinese,sjava String sql = "update student set sname=" + "'" + student.getsName() + "'" + " ," + " ssex=" + "'" + student.getsSex() + "'" + " ," + " scollege=" + "'" + student.getsCollege() + "' " + " ," + " sc=" + "'" + student.getsC() + "' " + " ," + " smath=" + "'" + student.getsMath() + "' " + " ," + " senglish=" + "'" + student.getsEnglish() + "'" + "," + " schinese=" + "'" + student.getsChinese() + "'" + " ," + " sjava=" + "'" + student.getsJava()+ "'" + " where" + " sid=" + "'"+ student.getsId() + "'"; System.out.println("------更新學生信息------"); System.out.println("sql語句:" + sql); boolean flag = false; try { flag = DBUtil.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return flag; } // 刪除學生信息 public boolean deleteStudentInfoBySid(String sid) { boolean flag = false; String sql = "delete from student where sid=" + "'" + sid + "'"; System.out.println("------刪除學生信息------"); System.out.println("sql語句:" + sql); DBUtil DBUtil = new DBUtil(); try { flag = DBUtil.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } return flag; } }
LoginFrame.java
package com.sjsq.view; import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.ImageIcon; 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; import javax.swing.Timer; import javax.swing.UIManager; import com.sjsq.model.User; import com.sjsq.util.MD5Util; import com.sjsq.dao.UserDao; public class LoginFrame extends JFrame implements ActionListener { public static String uname = null; public static String pwd = null; String username, password, login, quit; // 登錄標簽 private JLabel loginJLabel; private JPanel jContentPane = null; private JButton jButtonLogin = null; private JButton jButtonExit = null; private JTextField jTextFieldUserName = null; private JTextField jTextFieldPassWord = null; // 登錄用戶名 static int storeUserId; // 圖片 public JLabel jLabel_Image = null; // 登錄用戶名 public static String storeUserame = null; // 登錄密碼 public static String storeUserPassword = null; // 重新登陸標記 static boolean RELOAD = true; private JLabel jLabelUserName = null; private JLabel jLabelPassWord = null; int index; public LoginFrame() { // 設置背景 setForeground(new Color(255, 255, 255)); this.setResizable(false); this.setSize(800, 500); this.setTitle("學生成績管理系統"); this.setLocationRelativeTo(null); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// 使用windows外觀 } catch (Exception e) { e.printStackTrace(); } loginJLabel = new JLabel("學生成績管理系統"); Font font = new Font("微軟雅黑",Font.BOLD,40); loginJLabel.setFont(font); loginJLabel.setBounds(220,100,650, 50); loginJLabel.setForeground(Color.DARK_GRAY); getContentPane().add(loginJLabel); Font font2 = new Font("微軟雅黑",Font.BOLD,15); jLabelPassWord = new JLabel(); jLabelPassWord.setFont(font2); jLabelPassWord.setBounds(new Rectangle(300, 250, 71, 29)); jLabelPassWord.setText("用戶名:"); jLabelUserName = new JLabel(); jLabelUserName.setBounds(new Rectangle(300, 300, 71, 29)); jLabelUserName.setFont(font2); jLabelUserName.setText("密碼:"); // 賬號輸入框 jTextFieldUserName = new JTextField(20); jTextFieldUserName.setBounds(new Rectangle(360, 250, 154, 33)); // 密碼輸入框 jTextFieldPassWord = new JPasswordField(); jTextFieldPassWord.setBounds(new Rectangle(360, 300, 154, 33)); // 登錄 jButtonLogin = new JButton(); jButtonLogin.setBounds(new Rectangle(320, 380, 78, 26)); jButtonLogin.setText("登錄"); jButtonLogin.setUI(new BEButtonUI() .setNormalColor(BEButtonUI.NormalColor.green)); // 回車登錄 getRootPane().setDefaultButton(jButtonLogin); // 退出 jButtonExit = new JButton(); jButtonExit.setUI(new BEButtonUI() .setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonExit.setBounds(new Rectangle(420, 380, 78, 26)); jButtonExit.setText("退出"); // 包含所有的元素 jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(jLabelUserName, null); jContentPane.add(jLabelPassWord, null); jContentPane.add(jButtonLogin, null); jContentPane.add(jButtonExit, null); jContentPane.add(jTextFieldUserName, null); jContentPane.add(jTextFieldPassWord, null); getContentPane().add(jContentPane); jTextFieldUserName.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { if(e.getKeyChar() == KeyEvent.VK_ENTER){ jTextFieldPassWord.requestFocus(); } } }); jTextFieldPassWord.addKeyListener(new KeyListener() { @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyChar() == KeyEvent.VK_ENTER){ username = jTextFieldUserName.getText(); password = MD5Util.string2MD5(jTextFieldPassWord.getText()); User user = new User(); user.setUsername(username); user.setPassword(password); String s=user.getUsername(); String p=jTextFieldPassWord.getText(); UserDao userDao = new UserDao(); int choice = 0; if(!userDao.checkUser(user)) { JOptionPane.showMessageDialog(null, "用戶名和密碼錯誤","消息提示",JOptionPane.WARNING_MESSAGE); }else { setVisible(false); MainMenuFrame MM = new MainMenuFrame(); uname = s; pwd=p; } } } }); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); // 添加監控 jTextFieldUserName.addActionListener(this); jTextFieldPassWord.addActionListener(this); jButtonLogin.addActionListener(this); jButtonExit.addActionListener(this); } @Override public void actionPerformed(ActionEvent e) { // 獲取賬號和密碼 username = jTextFieldUserName.getText(); password = MD5Util.string2MD5(jTextFieldPassWord.getText()); User user = new User(); user.setUsername(username); user.setPassword(password); String s = user.getUsername(); String p = jTextFieldPassWord.getText(); UserDao userDao = new UserDao(); int choice = 0; if (e.getSource() == jButtonLogin) { if (!userDao.checkUser(user)) { JOptionPane.showMessageDialog(null, "用戶名和密碼錯誤", "消息提示", JOptionPane.WARNING_MESSAGE); } else { setVisible(false); MainMenuFrame MM = new MainMenuFrame(); uname = s; pwd = p; } } else if (e.getSource() == jButtonExit) { System.exit(0); } } // 測試 public static void main(String[] args) { new LoginFrame(); } }
MainMenuFrame.java
package com.sjsq.view; import java.awt.Font; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI; /* * 登陸成功後主界面 */ public class MainMenuFrame extends JFrame implements ActionListener { private JFrame mainmenu; private JTextArea text; // 標題欄(關於) private JMenuBar menuBar1; private JMenuItem aboutSystem; public MainMenuFrame() { mainmenu(); } public void mainmenu() { mainmenu = new JFrame("極客大學學生管理系統"); setLocationRelativeTo(null);// 將容器顯示在屏幕中央 mainmenu.setSize(850, 650); mainmenu.getContentPane().add(new JScrollPane(text)); JPanel jContentPane = new JPanel(); jContentPane.setLayout(null); // 查詢學生按鈕 JButton jButtonSelect = new JButton(); jButtonSelect.setBounds(new Rectangle(320, 100, 100, 52)); jButtonSelect.setText("查詢學生"); jButtonSelect.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); getRootPane().setDefaultButton(jButtonSelect);// 回車登錄 // 添加學生按鈕 JButton jButtonAdd = new JButton(); jButtonAdd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonAdd.setBounds(new Rectangle(320, 200, 100, 52)); jButtonAdd.setText("添加學生"); // 修改密碼按鈕 JButton jButtonAlterPwd = new JButton(); jButtonAlterPwd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonAlterPwd.setBounds(new Rectangle(320, 300, 100, 52)); jButtonAlterPwd.setText("密碼修改"); // 退出系統按鈕 JButton jButtonExit = new JButton(); jButtonExit.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue)); jButtonExit.setBounds(new Rectangle(320, 400, 100, 52)); jButtonExit.setText("退出系統"); jContentPane.add(jButtonSelect, null); jContentPane.add(jButtonAdd, null); jContentPane.add(jButtonAlterPwd, null); jContentPane.add(jButtonExit, null); // 添加Label到Frame mainmenu.getContentPane().add(jContentPane); // 設置字體 Font font = new Font("微軟雅黑", Font.BOLD, 16); // 標題欄 menuBar1 = new JMenuBar(); // aboutSystem = new JMenuItem("關於本系統",new // ImageIcon("src/images/icons/about.png")); aboutSystem = new JMenuItem("關於本系統"); aboutSystem.setMnemonic('H'); menuBar1.add(aboutSystem); // 查詢學生 jButtonSelect.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------查詢學生-----"); QueryStudentInfo queryStudentInfo = new QueryStudentInfo(); } }); // 添加學生 jButtonAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------添加學生-----"); AddStudentInfo addStudentInfo = new AddStudentInfo(); } }); // 修改密碼 jButtonAlterPwd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------修改密碼-----"); ModifyPasswordInfo modifyPasswordInfo = new ModifyPasswordInfo(); } }); // 退出系統 jButtonExit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------退出系統-----"); System.exit(1); } }); // 關於系統 aboutSystem.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("------關於系統------"); AboutSystem aboutSystem = new AboutSystem(); } }); // 關閉窗口監控 mainmenu.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { System.exit(1); } }); mainmenu.setJMenuBar(menuBar1); mainmenu.setVisible(true); // mainmenu.setLocation(250, 50); mainmenu.setLocationRelativeTo(null); aboutSystem.addActionListener(this); } // 主函數測試 public static void main(String[] args) { new MainMenuFrame(); } @Override public void actionPerformed(ActionEvent arg0) { } }
到此這篇關於Eclipse+Java+Swing實現學生成績管理系統的文章就介紹到這瞭,更多相關Eclipse+Java+Swing實現學生成績管理系統內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- sqlserver和java將resultSet中的記錄轉換為學生對象
- java swing 實現加載自定義的字體
- java修改JFrame默認字體方式
- Java如何重寫object類的equals方法詳解
- java控制臺實現學生信息管理系統(集合版)