package cn.itcast.jdbc;

import cn.itcast.util.JDBCUtils;

import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
* @author newcityman
* @date 2019/8/15 - 21:28
* 1、 通过键盘录入用户名和密码
* 2、 判断用户是否登录成功
*/
public class JDBCDemo08 {

public static void main(String[] args) {
System.out.println("请输入用户名:");
Scanner sc = new Scanner(System.in);
String username = sc.next();
System.out.println("请输入密码");
String password = sc.next();

/* boolean flag = new JDBCDemo08().login(username, password);
if (flag) {
System.out.println("登录成功");
} else {
System.out.println("用户名或密码错误,请联系系统管理员");
}*/

boolean f = new JDBCDemo08().login2(username, password);
if (f) {
System.out.println("登录成功");
} else {
System.out.println("用户名或密码错误,请联系系统管理员");
}

}

/*
*登录方法
*
* */

public boolean login(String username, String password) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
if (username == null || password == null) {
return false;
}
try {
// 1、 获取连接
conn = JDBCUtils.getConnection();
// 2、 定义sql
String sql = "select * from user where name='" + username + "'and password='" + password + "'";
System.out.println(sql);
// 3、 获取执行sql的对象
stmt = conn.createStatement();
// 4、执行查询
rs = stmt.executeQuery(sql);
// 5、判断
/*if (rs.next()) {
System.out.println("登录成功");
return true;
} else {
System.out.println("用户名或密码错误,请重新输入");
return false;
}*/
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, conn);
}
return false;
}

/*
*登录方法2:防止sql注入
*
* */

public boolean login2(String username, String password) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
if (username == null || password == null) {
return false;
}
try {
// 1、 获取连接
conn = JDBCUtils.getConnection();
// 2、 定义sql
String sql = "select * from user where name=? and password=?";
// 3、 获取执行sql的对象
stmt = conn.prepareStatement(sql);
stmt.setString(1,username);
stmt.setString(2,password);
// 4、执行查询
rs = stmt.executeQuery();
// 5、判断
/*if (rs.next()) {
System.out.println("登录成功");
return true;
} else {
System.out.println("用户名或密码错误,请重新输入");
return false;
}*/
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, conn);
}
return false;
}
}

最新文章

  1. [Java 基础]数据类型
  2. mysql5.6新特性总结
  3. JDBC学习笔记(2)
  4. 深入理解docker的link机制
  5. js Array对象
  6. ASP.NET中Web DataGrid的使用指南
  7. 5.1 Intent
  8. Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
  9. Sublime_调试PHP编译系统设置.
  10. eclipse打开时提示:failed to create the java virtual machine
  11. blog建表操作
  12. sprintf、strcpy 及 memcpy 函数区别
  13. JavaScript之深入理解【函数】
  14. Genymotion安卓模拟器和VirtualBox虚拟机安装、配置、测试
  15. SpringBoot------自动装配Mapper报错
  16. HW2017笔试编程题
  17. js Object.prototype.toString.call()
  18. 关于Java Logger类的使用问题 - 内存不释放
  19. qt cmake
  20. UDP与TCP笔记

热门文章

  1. macos proxy_bypass_macosx_sysconf exception
  2. 第三周PTA笔记 回文数+A-B(大数减法)+高精度除法+数楼梯(大数加法)
  3. 问题 C: A+B Problem II
  4. Django笔记&教程 6-2 表单(Form)基础操作
  5. jenkins内置变量的使用
  6. 力扣 - 剑指 Offer 22. 链表中倒数第k个节点
  7. TCP、三次握手、四次挥手(图解)
  8. freeswitch APR库哈希表
  9. Redis 很屌,不懂使用规范就糟蹋了
  10. DirectX12 3D 游戏开发与实战第八章内容(上)