1.用户登录
/*
* 用户登陆
*/
public class Login {
public static void main(String[] args) {
//接收用户输入的账号和密码
Scanner scanner = new Scanner(System.in);
System.out.println("请输入账号密码");
String name = scanner.nextLine();
System.out.println("请输入密码");
String password = scanner.nextLine();
//调用查询方法
User findUser = DoLogin.findUser(name, password);
if (findUser!=null) {
System.out.println("登陆成功");
}else {
System.out.println("账号密码不匹配");
}
//sql语句的注入问题添加一个恒成立的条件
//select *from users where name ='131313'
}
}
2.创建User类,类中的变量与数据库中的字段名一一对应
public class User {
//把对象保存到数组当中并遍历打印
//对象中声明的属性名尽量和数据库中的字段名相同
private int id;
private String password;
private String name;
private String email;
private Date birthday;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int id, String password, String name, String email, Date birthday) {
super();
this.id = id;
this.password = password;
this.name = name;
this.email = email;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBrithday() {
return birthday;
}
public void setBrithday(Date brithday) {
this.birthday = brithday;
}
@Override
public String toString() {
return "User [id=" + id + ", password=" + password + ", name=" + name + ", email=" + email + ", birthday="
+ birthday + "]";
}
}
3.写一个配置文件
driverClass=com.mysql.jdbc.Drive
url=jdbc:mysql://localhost:3306/myjdbc
user=root
password=123456
4.写一个读取配置文件和关闭数据库的方法
public class JDBCUtil {
//声明一个属性
private static String driverClass;
private static String url;
private static String user;
private static String password;
//使用静态代码块加载驱动 读取配置文件
static {
//使用系统类来读取 配置文件
ResourceBundle rb = ResourceBundle.getBundle("dbinfo");
//获取文件中的数据
driverClass = rb.getString("driverClass");
password = rb.getString("password");
url = rb.getString("url");
user = rb.getString("user");
//获取数据库链接的方法
public static Connection getConnection() throws SQLException, ClassNotFoundException {
return DriverManager.getConnection(url,user, password);
}
//关闭数据库的方法 如果没有结果集需要关闭 直接传空就可以
public static void closeALL(ResultSet executeQuery , Statement createStatement,Connection connection) {
if (executeQuery !=null) {
try {
executeQuery.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("关闭失败");
}
//加快系统回收的速度
executeQuery = null;
}
if (createStatement !=null) {
try {
createStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("关闭失败");
}
//加快系统回收的速度
executeQuery = null;
}
if (connection !=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new RuntimeException("关闭失败");
}
//加快系统回收的速度
executeQuery = null;
}
}
}
写一个登陆时要查询数据库的操作
/*
* 处理登陆的查询操作
*
*/
public class DoLogin {
//通过用户名密码 查找用户
public static User findUser(String name ,String password) {
String sql = "select*from users where name='"+name+"' and password='"+password+"'";
//创建User
User user = null;
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
//查询数据库
// 1. 注册驱动
try {
connection = JDBCUtil.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
//只返回一条数据
if (resultSet.next()) {
user = new User();
//使用user获取数据
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setEmail(resultSet.getString("email"));
user.setBrithday(resultSet.getDate("birthday"));;
}
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtil.closeALL(resultSet, statement, connection);
}
return user;
}
}