其他分享
首页 > 其他分享> > 登录案例

登录案例

作者:互联网

练习:登录案例

需求:
1.通过键盘录入用户名和密码
2.判断用户是否登录成功

select * from user where username = "" and password = ""

如果这个sql有查询结果,则成功,反之,则失败

步骤:
1.创建数据库表 user

create table user(
id int primary key auto_increment,
username varchar(32) not null,
password varchar(32) not null
);

insert into user values(null,"root","root");

select * from user;

工具类:

package cn.lhy.Utils;

import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC工具类
 */
public class JDBCUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driver;
    /**
     * 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块
     */
    static {
        try {
            //读取资源文件,获取值
            //1.创建Properties对象
            Properties pro = new Properties();

            //获取src路径下的文件方式--->ClassLoader 类加载器
            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
            URL res = classLoader.getResource("jdbc.properties");
            String path = res.getPath();
            //2.加载文件
            pro.load(new FileReader(path));
            //3.获取数据,赋值
            url = pro.getProperty("url");
            user = pro.getProperty("user");
            password = pro.getProperty("password");
            driver = pro.getProperty("driver");
            Class.forName(driver);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取连接
     * @return
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    /**
     * 释放资源
     * @param stmt
     * @param conn
     */
    public static void close(Statement stmt,Connection conn){
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    /**
     * 释放资源
     * @param stmt
     * @param conn
     */
    public static void close(ResultSet rs, Statement stmt, Connection conn){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (stmt!=null){
            try {
                stmt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
package cn.lhy.jdbc;

import cn.lhy.Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 需求:
 * 1.通过键盘录入用户名和密码
 * 2.判断用户是否登录成功
 */
public class Test {

    public static void main(String[] args) throws SQLException {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入账号和密码");
        //键盘录入接受用户名和密码
        String username = in.next();
        String password = in.next();
        //调用方法
        boolean login = login(username, password);
        //判断结果返回不同语句
        if (login) {
            System.out.println("登陆成功");
        } else {
            System.out.println("登录失败");
        }

    }

    /**
     * 登录方法
     */
    public static boolean login(String username, String password) {
        if (username == null || password == null || username == "" || password == "") {
            return false;
        }

        //连接数据库判断是否登陆成功
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //定义sql语句
        String sql = "select * from user where username = '" + username + "' and password = '" + password + "' ";
        //获取执行sql语句的对象
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //执行语句
        ResultSet rs = null;
        try {
            rs = stmt.executeQuery(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //判断
        /*if (rs.next()) {//如果有下一行则返回true即登陆成功
            return true;
        }else{
            return false;
        }*/

        try {
            return rs.next();//如果有下一行则返回true即登陆成功
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils.close(rs,stmt,conn);
        }
    }
}

标签:null,String,登录,stmt,案例,SQLException,password,conn
来源: https://www.cnblogs.com/ailhy/p/16538182.html