10.18每日总结
[实验任务二]:单一职责原则
登录模块在实际项目开发中很常见,请按照教材28页(PPT49页)利用单一职责原则重构后的类图实现这一模块。
实验要求:
1.提交源代码和对应的数据库文件(注意将此模块保存,以备以后使用);
2.注意编程规范。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | DBUtil.java: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/test1?serverTimezone=GMT%2B8&useSSL=false" ; public static String db_user = "root" ; public static String db_pass = "123456" ; public static Connection getConn () { Connection conn = null ; try { Class.forName( "com.mysql.cj.jdbc.Driver" ); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } //end getConn public static void close (Statement state, Connection conn) { if (state != null ) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null ) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null ) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null ; ResultSet rs = null ; String sql = "select * from user" ; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()){ System.out.println( "连接成功" ); } else { System.out.println( "连接失败" ); } } } UserDao.java: import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class UserDao { public boolean findUser(String username, String password) { //准备SQL语句 String sql = "select * from user where username ='" + username + "'" ; Connection conn= DBUtil.getConn(); //创建语句传输对象 Statement state = null ; ResultSet rs= null ; int flag= 0 ; String c_password= null ; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { ++flag; c_password=rs.getString( "password" ); } if (flag == 0 ) { return false ; } if (!password.equals(c_password)) { //判断密码 return false ; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return true ; } } LoginForm.java: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginForm extends JFrame { private static final long serialVersionUID = 1L; private UserDao dao = new UserDao(); //设置按钮组件 private JButton jb= new JButton( "登录" ); //添加一个登录按钮 private JButton button= new JButton( "重置" ); //添加一个确定按钮 //设置文本框组件 private JTextField username = new JTextField(); //用户名框 private JPasswordField password = new JPasswordField(); //密码框:为加密的*** JLabel user_name= new JLabel( "账号:" ); //设置左侧用户名文字 JLabel pass_word= new JLabel( "密码:" ); //设置左侧密码文字 public void init() { /* 组件绝对位置 */ user_name.setBounds( 50 , 70 , 300 , 25 ); pass_word.setBounds( 50 , 130 , 200 , 25 ); username.setBounds( 110 , 70 , 300 , 25 ); //设置用户名框的宽,高,x值,y值 password.setBounds( 110 , 130 , 300 , 25 ); //设置密码框的宽,高,x值,y值 button.setBounds( 315 , 225 , 90 , 20 ); //设置确定按钮的宽,高,x值,y值 jb.setBounds( 95 , 225 , 90 , 20 ); //设置确定按钮的宽,高,x值,y值 /* 组件透明化*/ user_name.setOpaque( false ); pass_word.setOpaque( false ); //监听事件 jb.addActionListener( new ActionListener(){ //为确定按钮添加监听事件 @SuppressWarnings ( "deprecation" ) public void actionPerformed(ActionEvent arg0) { validate(username.getText().trim(),password.getText().trim()); } }); //重置按钮 button.addActionListener( new ActionListener(){ //为重置按钮添加监听事件 //同时清空name、password的数据 public void actionPerformed(ActionEvent arg0) { // TODO 自动生成方法存根 username.setText( "" ); password.setText( "" ); } }); } public void display() { JFrame f = new JFrame(); f.setTitle( "登录页面" ); //窗口退出行为 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口大小不可变 f.setResizable( false ); //设置窗口打开居中 f.setLocationRelativeTo( null ); //窗口大小 f.setSize( 500 , 300 ); init(); //添加组件 Container contentPanel= new Container(); //添加一个contentPanel容器 contentPanel.setLayout( null ); //设置添加的contentPanel容器为流布局管理器 contentPanel.add(user_name); contentPanel.add(pass_word); contentPanel.add(username); contentPanel.add(password); contentPanel.add(jb); contentPanel.add(button); f.add(contentPanel); //展示窗口 f.setVisible( true ); } public void validate(String username,String password) { if (username.trim().length()== 0 ||password.trim().length()== 0 ){ JOptionPane.showMessageDialog( null , "用户名,密码不允许为空" ); return ; } if (dao.findUser(username, password)) { JOptionPane.showMessageDialog( null , "登录成功!" ); } else { JOptionPane.showMessageDialog( null , "用户名或密码错误" ); } } } Main.java: public class Main { public static void main(String[] args) { LoginForm loginForm= new LoginForm() ; //调用 loginForm.display(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | DBUtil.java: import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/test1?serverTimezone=GMT%2B8&useSSL=false" ; public static String db_user = "root" ; public static String db_pass = "123456" ; public static Connection getConn () { Connection conn = null ; try { Class.forName( "com.mysql.cj.jdbc.Driver" ); conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } //end getConn public static void close (Statement state, Connection conn) { if (state != null ) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null ) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null ) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null ; ResultSet rs = null ; String sql = "select * from user" ; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if (rs.next()){ System.out.println( "连接成功" ); } else { System.out.println( "连接失败" ); } } } UserDao.java: import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class UserDao { public boolean findUser(String username, String password) { //准备SQL语句 String sql = "select * from user where username ='" + username + "'" ; Connection conn= DBUtil.getConn(); //创建语句传输对象 Statement state = null ; ResultSet rs= null ; int flag= 0 ; String c_password= null ; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { ++flag; c_password=rs.getString( "password" ); } if (flag == 0 ) { return false ; } if (!password.equals(c_password)) { //判断密码 return false ; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return true ; } } LoginForm.java: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LoginForm extends JFrame { private static final long serialVersionUID = 1L; private UserDao dao = new UserDao(); //设置按钮组件 private JButton jb= new JButton( "登录" ); //添加一个登录按钮 private JButton button= new JButton( "重置" ); //添加一个确定按钮 //设置文本框组件 private JTextField username = new JTextField(); //用户名框 private JPasswordField password = new JPasswordField(); //密码框:为加密的*** JLabel user_name= new JLabel( "账号:" ); //设置左侧用户名文字 JLabel pass_word= new JLabel( "密码:" ); //设置左侧密码文字 public void init() { /* 组件绝对位置 */ user_name.setBounds( 50 , 70 , 300 , 25 ); pass_word.setBounds( 50 , 130 , 200 , 25 ); username.setBounds( 110 , 70 , 300 , 25 ); //设置用户名框的宽,高,x值,y值 password.setBounds( 110 , 130 , 300 , 25 ); //设置密码框的宽,高,x值,y值 button.setBounds( 315 , 225 , 90 , 20 ); //设置确定按钮的宽,高,x值,y值 jb.setBounds( 95 , 225 , 90 , 20 ); //设置确定按钮的宽,高,x值,y值 /* 组件透明化*/ user_name.setOpaque( false ); pass_word.setOpaque( false ); //监听事件 jb.addActionListener( new ActionListener(){ //为确定按钮添加监听事件 @SuppressWarnings ( "deprecation" ) public void actionPerformed(ActionEvent arg0) { validate(username.getText().trim(),password.getText().trim()); } }); //重置按钮 button.addActionListener( new ActionListener(){ //为重置按钮添加监听事件 //同时清空name、password的数据 public void actionPerformed(ActionEvent arg0) { // TODO 自动生成方法存根 username.setText( "" ); password.setText( "" ); } }); } public void display() { JFrame f = new JFrame(); f.setTitle( "登录页面" ); //窗口退出行为 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口大小不可变 f.setResizable( false ); //设置窗口打开居中 f.setLocationRelativeTo( null ); //窗口大小 f.setSize( 500 , 300 ); init(); //添加组件 Container contentPanel= new Container(); //添加一个contentPanel容器 contentPanel.setLayout( null ); //设置添加的contentPanel容器为流布局管理器 contentPanel.add(user_name); contentPanel.add(pass_word); contentPanel.add(username); contentPanel.add(password); contentPanel.add(jb); contentPanel.add(button); f.add(contentPanel); //展示窗口 f.setVisible( true ); } public void validate(String username,String password) { if (username.trim().length()== 0 ||password.trim().length()== 0 ){ JOptionPane.showMessageDialog( null , "用户名,密码不允许为空" ); return ; } if (dao.findUser(username, password)) { JOptionPane.showMessageDialog( null , "登录成功!" ); } else { JOptionPane.showMessageDialog( null , "用户名或密码错误" ); } } } Main.java: public class Main { public static void main(String[] args) { LoginForm loginForm= new LoginForm() ; //调用 loginForm.display(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix