package reflectionJdbc;
import org.apache.commons.beanutils.BeanUtils;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.*;
/*
* 数据库链接类
* */
public class jdbcUtils {
private static String driverClass;
private static String url;
private static String username;
private static String password;
/*
* 类文件加载阶段执行,而且只执行一次
* */
static {
//1.通过反射Class实例,获取类加载器,然后通过类加载器获取输入数据流对象
InputStream resourceAsStream = jdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//2.创建Properties实例对象,注:底层是Map键值对
Properties properties = new Properties();
//3.加载配置项文件
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
//4.将properties中的有效值提取出来
driverClass = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//5.加载驱动
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//6.释放资源
try {
if (resourceAsStream != null)
resourceAsStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 1.返回jdbc的链接
*
* @return Connection
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
/**
* 2.封装预处理 通用预处理模块 insert into stu(id,name,age)values(?,?,?,?)
*
* @param sql String
* @param connection Connection
* @param parameters 可变参数列表
* @return PreparedStatement
* @throws SQLException
*/
public static PreparedStatement getPrepareedStatement(String sql, Connection connection, Object... parameters) throws SQLException {
//获取预编译对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//通过预编译对象 获取元数据
ParameterMetaData parameterMetaData = preparedStatement.getParameterMetaData();
//通过元数据 获取字符串需要的参数个数
int parameterCount = parameterMetaData.getParameterCount();
//判断什么情况才需要给预编译对象赋值
if (parameterCount != 0 && parameters != null && parameters.length == parameterCount) {
for (int i = 0; i < parameterCount; i++) {
preparedStatement.setObject(i + 1, parameters[i]);
}
}
System.out.println("preparedStatement = " + preparedStatement);
return preparedStatement;
}
/**
* 3 释放链接资源
*
* @param connection
* @throws SQLException
*/
public static void close(Connection connection) throws SQLException {
connection.close();
}
/**
* 4.释放链接资源 Connection statement
*
* @param connection
* @param statement
* @throws SQLException
*/
public static void close(Connection connection, Statement statement) throws SQLException {
connection.close();
statement.close();
}
/**
* 5.释放链接资源 Connection Statement ResultSet
*
* @param connection Connection
* @param statement Statement
* @param resultSet ResultSet
* @throws SQLException
*/
public static void close(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
connection.close();
statement.close();
resultSet.close();
}
/**
* 6.数据更新功能
*
* @param sql String 自定义sql更新语句
* @param parameters 参数列表 更新数据
* @return int
* @throws SQLException
*/
public static int update1(String sql, Object... parameters) throws SQLException {
//获取链接
Connection connection = jdbcUtils.getConnection();
//预加载
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//获取字符串的个数
int parameterCount = preparedStatement.getParameterMetaData().getParameterCount();
//验证是否需要遍历给?赋值
if (parameterCount != 0 && parameters != null && parameters.length == parameterCount) {
for (int i = 0; i < parameterCount; i++) {
preparedStatement.setObject(i + 1, parameters[i]);
}
}
int i = preparedStatement.executeUpdate();
jdbcUtils.close(connection, preparedStatement);
return i;
}
/**
* 7.封装查询 思想:传入一个泛型类,然后将查询结果以指定类的形式返回
* * sql 泛型类 可变参数
* * BeanUtils可以帮助我们把属性封装到javaBean对象的对应属性中,比如
* * 可以把提交的表单数据封装到一个实体对象中。
* * 封装时要求参数名称和javaBeassn的属性名相同
*
* @param sql 根据给定的条件参数从表中查询数据
* @param cls 反射对象类
* @param parameters 查询条件参数列表
* @param <T> 任意类型
* @return 将查询结果以指定类的形式返回
* @throws SQLException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static <T> T queryBean(String sql, Class<T> cls, Object... parameters) throws SQLException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//1. 查询必要的参数
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
T t = null;
//2. 获取链接
connection = jdbcUtils.getConnection();
statement = getPrepareedStatement(sql, connection, parameters);
//3.执行查询操作,得到resultset集合
resultSet = statement.executeQuery();
//4.根据结果集合获取元数据
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
//实例化泛型约束之后对应的具体数据类型对象
t = cls.getConstructor().newInstance();
//字段个数 5
int columnCount = metaData.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
BeanUtils.setProperty(t, metaData.getColumnName(i), resultSet.getObject(i));
}
}
jdbcUtils.close(connection, statement, resultSet);
return t;
}
/**
* 8.思想:传入一个泛型类,以指定类的集合list形式返回
*
* @param sql 根据给定的条件参数从表中查询数据
* @param cls 反射对象类
* @param parameters 查询条件参数列表
* @param <T> 返回泛型集合
* @return
* @throws SQLException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static <T> List<T> queryBeanList(String sql, Class<T> cls, Object... parameters) throws SQLException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//1. 查询必要的参数
没有合适的资源?快使用搜索试试~ 我知道了~
学习JDBC第一个项目,封装JDBC
![preview](https://csdnimg.cn/release/downloadcmsfe/public/img/white-bg.ca8570fa.png)
共17个文件
jar:4个
xml:3个
java:3个
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
需积分: 0 0 下载量 84 浏览量
2023-12-04
21:09:47
上传
评论
收藏 3.01MB ZIP 举报
温馨提示
学习JDBC第一个练习,封装JDBC的源代码,初步封装,通过反射Class实例,类文件加载阶段执行,而且只执行一次的作封装,如:获取类加载器,然后通过类加载器获取输入数据流对象,创建Properties实例对象,加载配置项文件,将properties中的有效值提取出来,加载驱动,释放资源;然后封装返回资源,获取链接,封装预处理,返回预处理模块,释放连接资源,最后通过对增删改查方法的封装,以及返回的多种形式的方法的封装完成JDBC的封装学习
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![wmv](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 17 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/12f9dab4ff8848e2a289ad259852380c_weixin_68929783.jpg!1)
道爷我悟了
- 粉丝: 249
- 资源: 11
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)