开头打广告,Java1234.com。
首先,来个效果图。
关键代码
1,界面层
private void Jb_DengLuActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:String UserName = this.Jb_UserNameTxt.getText();String Password = new String(this.Jb_PasswordTxt.getPassword());if (StringUtil.IfEmpty(UserName)) {JOptionPane.showMessageDialog(null, "用户命不能为空!");} else if (StringUtil.IfEmpty(Password)) {JOptionPane.showMessageDialog(null, "密码不能为空!");}try {User user = new User(UserName, Password);Connection con = null;con = dbutil.getcon();User userquery = usdao.loginOn(con, user);if (userquery != null) {//JOptionPane.showMessageDialog(null, "用户成功登录!");this.dispose();MainFrm mainfrm = new MainFrm();mainfrm.setVisible(true);} else {JOptionPane.showMessageDialog(null, "用户登录失败");}} catch (Exception e) {e.printStackTrace();}}private void Jb_RetActionPerformed(java.awt.event.ActionEvent evt) {// TODO add your handling code here:this.Jb_UserNameTxt.setText("");this.Jb_PasswordTxt.setText("");}
2,数据连接层
public User loginOn(Connection con, User user){User Resultuser=new User() ;String sql="select*from tb_user where UserName=? and passWord=? ";try {PreparedStatement pstm=con.prepareStatement(sql);pstm.setString(1, user.getUserName());pstm.setString(2, user.getPassword());ResultSet rs= pstm.executeQuery();while(rs.next()){Resultuser.setUserName(rs.getString("UserName"));Resultuser.setPassword(rs.getString("passWord")); } } catch (Exception e) {// TODO Auto-generated catch block e.printStackTrace();}return Resultuser;}