JDBC高级操作

 模拟淘宝登录功能

 后台返回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

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值