package com.study.util;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/
* 连接,驱动连接,关闭都有,
* 作为通用的DML处理
*/
public class DBUtil {
static String url="jdbc:mysql://127.1.0.1:3306/mytest?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia%2fShanghai";
static String username="root";
static String userpass="";
static String driver="com.mysql.cj.jdbc.Driver";
static{
//从配置文件获取以上信息
Properties properties=new Properties();
InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
properties.load(is);//将jdbc.properties加载到这个对象
} catch (IOException e) {
e.printStackTrace();
}
//获取到配置文件里的信息
url = properties.getProperty("url");
username = properties.getProperty("name");
userpass = properties.getProperty("password");
driver = properties.getProperty("driver");
//注册驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//连接方法
public static Connection getConnection(){
Connection connection=null;
if (null==connection){
try {
connection= DriverManager.getConnection(url,username,userpass);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return connection;
}
//关闭方法
public static void close(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection){
try {
if (resultSet!=null) {
resultSet.close();
}
if (preparedStatement!=null) {
preparedStatement.close();
}
if (connection!=null) {
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/
* DML通用方法
* @param sql
* @param params,Object... params 可变长的参数
* @return boolean
*/
public static boolean executeDML(String sql,Object... params){
try {
//获取连接
Connection connection=getConnection();
//预处理
PreparedStatement ps=connection.prepareStatement(sql);
//设置参数
if (null!=params){
for (int i=0;i<params.length;i++){
ps.setObject(i+1,params[i]);
}
}
//执行SQL
int i = ps.executeUpdate();
//关闭连接
close(null,ps,connection);
return i>0;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return false;
}
/
* 通用的DQL方法
* @param sql 查询语句
* @param tClass 实体类的字节码对象
* @param params 查询语句里的占位符
* @param <T> 实体类的泛型
* @return 查询到的数据集合
*/
public static <T> List<T> executeDQL(String sql,Class<T> tClass,Object... params){
try {
//1.获取连接
Connection connection=getConnection();
//2.预处理
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//3.设置参数
if (params!=null){
for (int i=0;i<params.length;i++){
preparedStatement.setObject(i+1,params[i]);
}
}
//4.执行SQL得到结果集
ResultSet resultSet = preparedStatement.executeQuery();
/
* 5.遍历并打印结果集
*/
//用List来保存所有的表数据
List<T> list=new ArrayList<T>();
T t;
//获取原数据
ResultSetMetaData metaData = resultSet.getMetaData();
//获取列的总数
int columnCount = metaData.getColumnCount();
//循环遍历结果
while(resultSet.next()){
//用t保存每一条数据
t=tClass.newInstance();
for (int i=1;i<=columnCount;i++){
//获取每列的别名
String columnLabel = metaData.getColumnLabel(i);
//获取该列是所有行信息
Object object = resultSet.getObject(columnLabel);
//打印该列的所有行数据
try {
//根据属性的名称通过反射获取属性对象
Field declaredField = tClass.getDeclaredField(columnLabel);
declaredField.setAccessible(true);
declaredField.set(t,object);
}catch (NoSuchFieldException e) {
//代表我们当前的实体类里面没有这个属性
}
}
list.add(t);
}
//关闭连接
close(resultSet,preparedStatement,connection);
//将t对象保存到list里面
return list;
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return null;
}
今天的文章 sql增删改查(SQL增删改查编写程序)分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/40231.html