“=”和“==”的坑

今天写了个预编译的数据查询,坑的一批,一次次DEBUG才解决,看代码吧!
JDBC工具类

public class JDBCUtilDemo03 {
public static Connection getConnection() throws Exception {
	//	1.加载驱动
		
		Properties properties=new Properties();
		properties.load(new FileInputStream("db.properties"));
		//获取各种属性
		String user=properties.getProperty("user");
		String password=properties.getProperty("password");
		String url=properties.getProperty("url");
		String driverClass=properties.getProperty("driverClass");
		//注册驱动
		Class.forName(driverClass);
		Connection conn=DriverManager.getConnection(url, user, password);

		return conn;
	}
}

登入工具类

public class LoginUtil {
public static boolean checkLoginPro(String id,String password) throws Exception {
	Connection conn=JDBCUtilDemo03.getConnection();
	String sql = "select * from user where id=? and password=?";
	PreparedStatement pst=conn.prepareStatement(sql);
	pst.setNString(1, id);
	pst.setNString(2, password);
    ResultSet rs=pst.executeQuery();
    
    boolean result=rs.next();
    
	JDBCUtilDemo03.releaseSourse(conn,pst,rs);
	return result;
	}
	}

代码实现:

public class JDBCDemo3 {

	public static void main(String[] args) throws Exception {

		/**
		 * 编写登录案例 用户输入用户名和密码 连接数据库,查询用户输入的内容是否存在
		 */
		// 获取用户名和密码
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String id = sc.next();

		System.out.println("请输入密码:");
		String paw = sc.next();

		Properties properties = new Properties();
		properties.load(new FileInputStream("db.properties"));
		String url = properties.getProperty("url");
		String user = properties.getProperty("user");
		String pwd = properties.getProperty("password");
		Connection conn = DriverManager.getConnection(url, user, pwd);


		// 导入loginUtil类中的方法,但还可以执行注入
		boolean result=LoginUtil.checkLoginPro(id, paw);
		
          System.out.println(result);
          /*在这里,我把判断写成了“=”相当于再次赋值,
          不管查什么都是登入成功*/
		if (result==true) {
			
			System.out.println("登陆成功!");
		} else {
			System.out.println("用户名或者密码错误!");
		}
		// 释放资源
		
		conn.close();
	}

}

以此未戒吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值