• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
百里丶落云
Gee,gee,baby,baby
            管理     
jdbc连接2(不可以注入)
public void login1(String username, String password) throws ClassNotFoundException, SQLException {
	// 1.注册驱动
	Class.forName("com.mysql.jdbc.Driver");
	// 2.获取连接
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "root");
	// 3.编写sql语句
	String sql = "select * from tbl_user where uname=? and upassword=?";
	// 4.创建预处理对象
	PreparedStatement pstmt = conn.prepareStatement(sql);
	// 5.设置参数(给占位符)
	pstmt.setString(1, username);
	pstmt.setString(2, password);
	// 6.执行查询操作
	ResultSet rs = pstmt.executeQuery();
	// 7.对结果集进行处理
	if (rs.next()) {
		System.out.println("恭喜您," + username + ",登录成功!");
		System.out.println(sql);
	} else {
		System.out.println("账号或密码错误!");
	}
	if (rs != null)
		rs.close();
	if (pstmt != null)
		pstmt.close();
	if (conn != null)
		conn.close();
}
}

  

年与时驰,意与日去,遂成枯落, 多不接世,悲守穷庐,将复何及。
posted on 2017-12-10 22:13  百里丶落云  阅读(114)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3