一、系统介绍
本系统实现了用户登录,实现了对学生信息的增删改查,实现了用户修改密码功能,采用MD5加密算法,数据库使用Mysql8.0.13,界面良好。
二、系统展示
1.登录页
2.主页面
3.关于本系统
4.查询所有
5.按条件查询
6.修改学生信息
7.添加学生信息
8.修改密码
三、部分代码
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 sMajor; // 出生日期 private String sBirthday; // 省份 private String sProvince; // 电话 private String sPhone; // 邮箱 private String sEmail; public Student() { super(); } public Student(String sId, String sName, String sSex, String sCollege, String sMajor, String sBirthday, String sProvince, String sPhone, String sEmail) { super(); this.sId = sId; this.sName = sName; this.sSex = sSex; this.sCollege = sCollege; this.sMajor = sMajor; this.sBirthday = sBirthday; this.sProvince = sProvince; this.sPhone = sPhone; this.sEmail = sEmail; } 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 getsMajor() { return sMajor; } public void setsMajor(String sMajor) { this.sMajor = sMajor; } public String getsBirthday() { return sBirthday; } public void setsBirthday(String sBirthday) { this.sBirthday = sBirthday; } public String getsProvince() { return sProvince; } public void setsProvince(String sProvince) { this.sProvince = sProvince; } public String getsPhone() { return sPhone; } public void setsPhone(String sPhone) { this.sPhone = sPhone; } public String getsEmail() { return sEmail; } public void setsEmail(String sEmail) { this.sEmail = sEmail; } @Override public String toString() { return "Student [sId=" + sId + ", sName=" + sName + ", sSex=" + sSex + ", sCollege=" + sCollege + ", sMajor=" + sMajor + ", sBirthday=" + sBirthday + ", sProvince=" + sProvince + ", sPhone=" + sPhone + ", sEmail=" + sEmail + "]"; } }
User.java
package com.sjsq.model; /** * User实体类 * * @author shuijianshiqing * */ public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [username=" + username + ", password=" + password + "]"; } }
UserDao.java
package com.sjsq.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.sjsq.model.User; import com.sjsq.util.DBUtil; import com.sjsq.util.MD5Util; import com.sjsq.view.LoginFrame; /** * 实现User登录验证及修改密码功能 * @author shuijianshiqing * */ public class UserDao { // 判断这个用户是否存在 public boolean checkUser(User user){ boolean existFlag=false; List<User> list=query(user); // list的size大于0,则用户存在 if(list.size()>0){ existFlag=true; } return existFlag; } // 查询是否有这个用户,并且返回这个用户 public List<User> query(User user){ List<User> list=new ArrayList<User>(); String sql="select * from user where 1=1 "; // 判断username是否为空 if(user.getUsername()!=null){ sql+=" and username='"+user.getUsername()+"' "; } // 判断password是否为空 if(user.getPassword()!=null){ sql+=" and password='"+user.getPassword()+"' "; } System.out.println("------验证用户信息------"); System.out.println("sql语句:"+sql); DBUtil db=new DBUtil(); ResultSet rs=null; try { rs=db.executeQuery(sql); while(rs.next()){ // 保存用户信息到list中 User userTmp=new User(); userTmp.setUsername(rs.getString("username")); userTmp.setPassword(rs.getString("password")); list.add(userTmp); } } catch (SQLException e) { e.printStackTrace(); } finally{ try { if(rs!=null){ rs.close(); } } catch (SQLException e) { e.printStackTrace(); } // 关闭数据库 db.close(); } return list; } // 修改密码 public void modifypassword(String password) throws SQLException { // 获取uname String username=LoginFrame.uname; // 密码加密 String s=MD5Util.string2MD5(password); String sql="update user set password='"+s+"'where username="+"'"+username+"'"; DBUtil db=new DBUtil(); db.executeUpdate(sql); db.close(); } }
package com.sjsq.util; /** * 字节操作 * @author shuijianshiqing * */ public class ByteUtil { public static String string2Byte(String str) { // 定义数组 byte[] bytes = str.getBytes(); return bytes.toString(); } public static void main(String[] args) { String str = "123456"; System.out.println(string2Byte(str)); } }
DBUtil.java
package com.sjsq.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * 实现JDBC的封装 * @author shuijianshiqing * */ public class DBUtil { //成员变量 private Connection con; private Statement st; private ResultSet rs; private String dirver; private String url; private String username; private String password; // 构造函数:实现加载数据库驱动,建立连接,创建语句对象 public DBUtil() { Properties properties = new Properties(); // 获取当前项目根路径 FileUtil fileUtil = new FileUtil(); String filePath = fileUtil.getProperty(); try { // 把文件转化为输入流 FileInputStream fileInputStream = new FileInputStream(filePath+"/src/resources/db.properties"); // 加载输入流 properties.load(fileInputStream); // 获取对应参数 dirver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); // 加载驱动 Class.forName(dirver); // 创建连接 con = DriverManager.getConnection(url, username, password); // 创建语句对象 st = con.createStatement(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } // 执行insert,update,delete操作 public boolean executeUpdate(String sql) throws SQLException{ boolean returnFlag=false; int i=st.executeUpdate(sql); // 判断操作行数是否大于0 if(i>0){ returnFlag=true; } return returnFlag; } // 执行select操作 public ResultSet executeQuery(String sql) throws SQLException{ rs=st.executeQuery(sql); // 返回操作结果 return rs; } // 释放资源 public void close(){ try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); }finally { } } public static void testSelect() { // 测试数据库连接 DBUtil dbUtil = new DBUtil(); String sql = "select * from test"; try { ResultSet rs = dbUtil.executeQuery(sql); while(rs.next()) { String username = rs.getString("username"); String password = rs.getString("password"); System.out.println(username+"-->"+password); } } catch (SQLException e) { e.printStackTrace(); } dbUtil.close(); } public static void testInsert() { // 测试数据库连接 DBUtil dbUtil = new DBUtil(); String sql = "insert into test values('sjsq1','123456')"; try { Boolean executeFlag = dbUtil.executeUpdate(sql); if (executeFlag) { System.out.println("插入数据成功"); }else { System.out.println("插入数据失败"); } } catch (SQLException e) { e.printStackTrace(); } dbUtil.close(); } // 测试 public static void main(String[] args) { // 获取当前路径 FileUtil fileUtil = new FileUtil(); String filePath = fileUtil.getProperty(); System.out.println(filePath); testSelect(); } }
MD5Util.java
package com.sjsq.util; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 采用MD5加密解密 * @author shuijianshiqing * */ public class MD5Util { // MD5加密 public static String string2MD5(String source){ // 获取MessageDigest对象 MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } // 执行加密操作 byte[] bytes = source.getBytes(); // MD5算法:得到的目标字节数组的特点,长度固定为16 byte[] md5Bytes = messageDigest.digest(bytes); // 十六进制的字符 char[] chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7' , '8', '9', 'A', 'B', 'C', 'D', 'E','F' }; StringBuffer sb = new StringBuffer(); // 处理成十六进制的字符串(通常) for (byte bb : md5Bytes) { sb.append(chars[(bb >> 4) & 15]); sb.append(chars[bb & 15]); } return sb.toString(); } // 测试主函数 public static void main(String args[]) { System.out.println(string2MD5("123456")); } }
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(180,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(); } }