栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC高级操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {
    
    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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/584277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号