JDBC工具类实现登录功能

网友投稿 610 2022-12-31

JDBC工具类实现登录功能

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/studentrEjzWCTh

username=root

password=root

driver=com.mysql.jdbc.Driver

JDBC工具类

package cn.itcast.util;

import java.io.FileReader;

import java.io.IOException;

import java-.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、通过键盘录入用户名和密码

http://* 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、调用方法

http:// 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;

}

}

运行结果:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:app分享微信小程序(小程序中分享小程序)
下一篇:政务一网通办项目(政务大厅一网通办)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~