DBUtils工具类介绍
DBUtils是java编程中的数据库操作使用工具。DBUtils封装了对JDBC的操作,简化了JDBC的操作
IRowMapper接口
package com.lanou.util;
import java.sql.ResultSet;
@FunctionalInterface
public interface IRowMapper {
void rowMapper(ResultSet resultSet);
}
DBUTil类(核心)
介绍
· static{}:加载JDBC驱动类
·getConnection(){}:获取数据库连接
·close(){}:关闭JDBC对象,释放资源(封装了释放各种类型对象)
·Update,exist,select用来完成表数据的更新,添加,删除操作,Object...params可变参数
package com.lanou.util;
import java.sql.*;
public class DBUtil {
static {
//1.加载驱动类
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static Connection getConnection() throws SQLException {
String url = PropertiesUtil.value("url");
String userName=PropertiesUtil.value("userName");
String password=PropertiesUtil.value("password");
return DriverManager.getConnection(url,userName,password);
}
private static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
private static void close(PreparedStatement preparedStatement, Connection connection) {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
private static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(statement,connection);
}
private static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
close(preparedStatement,connection);
}
public static boolean executeUpdate(String sql) {
Connection connection = null;
Statement statement = null;
try {
connection = getConnection();
statement = connection.createStatement();
int num = statement.executeUpdate(sql);
return num > 0;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(statement, connection);
}
return false;
}
//查询语句
public static void select(String sql, IRowMapper rowMapper) {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String url = "jdbc:mysql://127.0.0.1:3306/venus?useSSL=false";
try {
//2.获取Mysql连接
connection = getConnection();
//3.创建语句
statement = connection.createStatement();
//4.执行SQL语句
resultSet = statement.executeQuery(sql);
//5.处理结果
rowMapper.rowMapper(resultSet);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(resultSet, statement, connection);
}
}
public static void select(String sql, IRowMapper rowMapper, Object... params) {
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
Connection connection = null;
try {
//2.获取连接
connection = getConnection();
//4.执行sql
//预编译
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject((i + 1), params[i]);
}
resultSet = preparedStatement.executeQuery();
rowMapper.rowMapper(resultSet);
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//释放资源
//preparedStatement.close();
close(resultSet,preparedStatement,connection);
}
}
//判断用户名是否存在
public static boolean exist(String sql, Object... params) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String url = "jdbc:mysql://127.0.0.1:3306/venus?useSSl=false";
try {
//2获取连接
connection = getConnection();
//3创建语句
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject((i+1),params[i]);
}
//4执行SQL
resultSet = preparedStatement.executeQuery();
//5处理结果
return resultSet.next();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
close(resultSet, preparedStatement, connection);
}
return false;
}
//更新语句
public static boolean update(String sql, Object... params) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
int effect = preparedStatement.executeUpdate();
return effect > 0;
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//释放资源
close(preparedStatement, connection);
}
return false;
}
}
PropertiesUtil文件
·为了后期便于配置管理软件,,常将诸如数据库连接配置(url、用户名和密码)、上传文件保存路径等配置信息写在properties文件中。
1src根目录创建properties类型文件,如下图所示
db.propertise
url=jdbc:mysql://127.0.0.1:3306/venus?useSSL=false
user_name=root
password=root
2.2.创建Properties类对象;
3.调用Properties类对象load方法加载properties类型文件;
4.调用Properties类对象getProperty方法获取properties类型文件中key所对应的数据;
PropertiseUtil
package com.lanou.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
private static Properties properties = new Properties();
static{
//从类加载路径取得文件的输入流,注意不能以/开头
InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties.load(inputStream);//加载properties类型文件
} catch (IOException e) {
e.printStackTrace();
}
}
public static String value(String key) {
return properties.getProperty(key);
}
}
private static Connection getConnection() throws SQLException {
//通过getProperty方法获取properties类型文件中key所对应的数据
String url = PropertiesUtil.value("url");
String userName=PropertiesUtil.value("userName");
String password=PropertiesUtil.value("password");
return DriverManager.getConnection(url,userName,password);
}
StringUtil工具类MD5加密 获取UUID
package com.lanou.util;
import org.apache.commons.codec.digest.DigestUtils;
import java.util.UUID;
public class StringUtil {
public static String getId(){
return UUID.randomUUID().toString();
}
public static String getMD5(String md5){
return DigestUtils.md5Hex(md5);
}
}
MD5加密
//MD5加密
DigestUtils.md5Hex(name);