swing之登陆功能

       看完http://www.java1234.com/a/yuanchuang/swing/上面的教学视频,我尝试开发了一个简单的学院信息管理,下面是我的开发过程。

    首先建立四个包,分别为:

           1、com.java. util,它里面包含数据库连接和基本的一个判空类;

           2、com.java.view,  里面是一些界面;

          3、com.java.model,封装一些成员属性和成员方法;

          4 、com.java.dao,   封装数据库的一些增删改查的方法;

此次项目所具有的的功能主要有登陆、注册新账号,接着进入主界面,下来进入主界面,主界面的功能结构如下:

       

  首先,需要实现登陆功能,登陆验证即查询用户名和密码是否正确,数据库查询得代码如下:

public ResultSet selectUser(Connection con,String userName,String password) throws Exception{
        String sql="select * from t_user where userName=? and password=?";
        PreparedStatement pst=con.prepareStatement(sql);
        pst.setString(1, userName);
        pst.setString(2,password);
    return pst.executeQuery();
        
    
    }

登陆验证代码如下:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        String userName = jusername.getText();
        String password = new String(jpassword.getPassword());
        if (Strutil.isEmpty(userName)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
            return;
        }
        if (Strutil.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
            return;
        }
        Connection con = null;
        try {
            con = datacon.getCon();
            ResultSet rs = userdao.selectUser(con, userName, password);
            if (rs.next()) {
                this.dispose();
                new mainFrm().setVisible(true);
                JOptionPane.showMessageDialog(null, "欢迎访问!");

            } else {
                JOptionPane.showMessageDialog(null, "用户名或密码错误");

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "登录失败");

        } finally {
            datacon.closeCon(con);
        }

    }


对于注册用户名,即用数据库的添加功能:

   

public int AddUser(Connection con,String userName,String password) throws Exception{
        String sql="insert into t_user values(null,?,?)";
        PreparedStatement pst=con.prepareStatement(sql);
        pst.setString(1,userName);
        pst.setString(2,password);
        return pst.executeUpdate();
    }

对于判断注册是否成功,首先应检查数据库是否存在注册的用户名,如果存在即要输出“该用户已存在,请重新注册!”,否则注册成功,进入主界面,代码如下:

public boolean getUserName(Connection con,String currentuserName)throws Exception{
		String sql="select * from t_user where userName=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, currentuserName);
		ResultSet rs=pstmt.executeQuery();
		return rs.next();
	}

 下面代码为确定是否注册成功:
             

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		String userName = this.juserNametxt.getText();
		String password = this.jpasswordtxt.getText();
		if (Strutil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空!");
			return;
		}
		if (Strutil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空!");
			return;
		}
		Connection con = null;
		try {
			con = datacon.getCon();
			boolean flag = userdao.getUserName(con, userName);
			if (flag) {
				JOptionPane.showMessageDialog(null, "此用户名已经存在,请重新注册!");
				return;
			}
			int addNum = userdao.AddUser(con, userName, password);
			if (addNum == 1) {
				JOptionPane.showMessageDialog(null, "恭喜你,注册成功!");
				this.dispose();
				new mainFrm().setVisible(true);
				JOptionPane.showMessageDialog(null, "欢迎访问!");
			} else {
				JOptionPane.showMessageDialog(null, "注册失败!");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "注册失败!");
		} finally {
			datacon.closeCon(con);
		}

	}

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
		new LogonFrm().setVisible(true);
	}

	private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
		this.juserNametxt.setText("");
		this.jpasswordtxt.setText("");
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new UserFrm().setVisible(true);
			}
		});

源码分享:http://pan.baidu.com/share/link?shareid=2572197744&uk=2048116807

原文地址:https://www.cnblogs.com/zhuo1/p/3209475.html