JDBC工具类实现登录功能

本文实例为大家分享了JDBC工具类实现登录功能的具体代码,供大家参考,具体内容如下

我们使用JDBC实现数据库的增删改查,代码基本差不多,有很多重复,所以我们可以把这些重复的代码写成一个工具类,使用的时候直接调用就可以了。下面以实现登录功能的案例来介绍。

创建数据库,插入数据

use student;create table user( id int primary key auto_increment,    username varchar(32),    password varchar(32));insert into user values(null,'zhangsan','123');insert into user values(null,'lisi','234');

jdbc.properties

url=jdbc:mysql://localhost:3306/studentusername=rootpassword=rootdriver=com.mysql.jdbc.Driver

JDBC工具类

package cn.itcast.util;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 username;    private static String password;    private static String driver;    /**     * 文件的读取,著需要读取一次即可拿到这些值,使用静态代码块     **/    static {        try {            //1、读取资源文件,获取值            Properties properties = new Properties();            //获取src路径下的文件的方式 ---> ClassLoader类加载器            ClassLoader classLoader = JDBCUtils.class.getClassLoader();            URL resource = classLoader.getResource("jdbc.properties");            String path = resource.getPath();            //2、加载文件            properties.load(new FileReader(path));            //3、获取数据,赋值            url = properties.getProperty("url");            username = properties.getProperty("username");            password = properties.getProperty("password");            driver = properties.getProperty("driver");            //4、注册驱动            Class.forName(driver);        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    /**     * 获取连接     * @return 连接对象     */    public static Connection getConnection() throws SQLException {        return DriverManager.getConnection(url,username,password);    }    /**     * 释放资源     * @param statement     * @param connection     */    public static void close(Statement statement,Connection connection) {        if (statement != null) {            try {                statement.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (connection != null) {            try {                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }    /**     * 释放资源     * @param resultSet     * @param statement     * @param connection     */    public static void close(ResultSet resultSet,Statement statement, Connection connection) {        if (resultSet != null) {            try {                resultSet.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (statement != null) {            try {                statement.close();            } catch (SQLException e) {                e.printStackTrace();            }        }        if (connection != null) {            try {                connection.close();            } catch (SQLException e) {                e.printStackTrace();            }        }    }}

实现登录功能

package cn.itcast.jdbc;import cn.itcast.util.JDBCUtils;import java.sql.*;import java.util.Scanner;/** *  1、通过键盘录入用户名和密码 *  2、判断用户是否登录成功 */public class JDBCLogin {    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、调用方法        boolean flag = new JDBCLogin().login(username,password);        //3、判断        if (flag) {            System.out.println("登录成功");        } else {            System.out.println("用户名或密码错误!");        }    }    /**     * 登录方法     */    public boolean login(String username,String password) {        if (username == null || password == null) {            return false;        }        //连接数据库判断是否登陆成功        Connection connection = null;        PreparedStatement preparedStatement = null;        ResultSet resultSet = null;        try {            //1、获取链接            connection = JDBCUtils.getConnection();            //2、定义sql            String sql = "select * from user where username = ? and password = ?";            //3、获取执行sql的对象            //为了防止sql注入,实现事务安全,效率更高,必须要用PreparedStatement            preparedStatement = connection.prepareStatement(sql);            //给?赋值            preparedStatement.setString(1,username);            preparedStatement.setString(2,password);            //4、执行查询,不需要传递sql            resultSet = preparedStatement.executeQuery();            //5、判断:如果是下一行,则返回true            return resultSet.next();        } catch (SQLException e) {            e.printStackTrace();        } finally {            JDBCUtils.close(resultSet,preparedStatement,connection);        }        return false;    }}

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

竞争颇似打网球,与球艺胜过你的对手比赛,

JDBC工具类实现登录功能

相关文章:

你感兴趣的文章:

标签云: