本文实例为大家分享了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/student
username=root
password=root
driver=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;
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();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
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();
}
}
}
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;
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;
}
}
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。