模拟淘宝登录功能
后台返回User类的数据,前台处理后台返回的数据,如果为空说明登录失败
public static void main(String[] args) {
//1.输入用户名和密码
Scanner sc =new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
//2.调用后台判断登录是否成功并返回结果给前台
User user=login(username,password);
//3.在前台输出结果
if(user==null){
System.out.println("登陆失败");
}else{
System.out.println("欢迎你:真实姓名");
}
}
创建表,往其中添加数据
create table t_user(
userid varchar(10) primary key,
realname varchar(3) not null,
password varchar(6) not null,
money double(10,2)
);
insert into t_user values('zhangsan','张三','zhangs',5000);
insert into t_user values('lisi','李四','lis',5000);
select * from t_user;
后台代码
public static User login(String userId,String pwd){
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
User user2=null;
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true";
String user = "root";
String password = "";
//加载驱动
Class.forName(driver);
//与数据库建立连接
conn = DriverManager.getConnection(url, user, password);
//建立sql命令发送器
stmt = conn.createStatement();
//准备一个sql命令,并用sql发送器发送过去,并返回结果
String sql = "select * from t_user where userid='"+userId+"'" +
" and password='"+pwd+"'";
rs = stmt.executeQuery(sql);
//处理结果(将JDBC的内容放到List集合中)
if(rs.next()){
//获取当前行各个字段的值
String realName=rs.getString("realname");
Double money=rs.getDouble("money");
user2=new User(userId,realName,password,money);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
//释放资源
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return user2;
}
完整代码
package com.JDBC2;
import com.JDBC.entity.Emp;
import com.JDBC2.entity.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* 模拟淘宝登录的功能
*/
public class TestLogin {
/**
* 前台
* @param args
*/
public static void main(String[] args) {
//1.输入用户名和密码
Scanner sc =new Scanner(System.in);
System.out.println("请输入用户名:");
String userId = sc.nextLine();
System.out.println("请输入密码:");
String password = sc.nextLine();
//2.调用后台判断登录是否成功并返回结果给前台
User user=login(userId,password);
//3.在前台输出结果
if(user==null){
System.out.println("登陆失败");
}else{
System.out.println("欢迎你:"+user.getRealName());
}
}
public static User login(String userId,String pwd){
Connection conn = null;
Statement stmt = null;
ResultSet rs=null;
User user2=null;
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true";
String user = "root";
String password = "";
//加载驱动
Class.forName(driver);
//与数据库建立连接
conn = DriverManager.getConnection(url, user, password);
//建立sql命令发送器
stmt = conn.createStatement();
//准备一个sql命令,并用sql发送器发送过去,并返回结果
String sql = "select * from t_user where userid='"+userId+"'" +
" and password='"+pwd+"'";
rs = stmt.executeQuery(sql);
//处理结果(将JDBC的内容放到List集合中)
if(rs.next()){
//获取当前行各个字段的值
String realName=rs.getString("realname");
Double money=rs.getDouble("money");
user2=new User(userId,realName,password,money);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
//释放资源
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return user2;
}
}
statement有注入问题,是不安全的所以建议用PreparedStatement