package com.tmg.util;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class JDBCUtils {
static DataSource dataSource = null;
static {
Properties properties = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("durid.properties");
// InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("durid.properties");
// System.out.println(resourceAsStream);
try {
properties.load(is);
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取连接
public static Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("获取连接失败");
}
return connection;
}
// 查询
public static <B> List<B> query(Class<B> clazz,String sql,Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet =null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
preparedStatement.setObject(i + 1, args[i]);
}
System.out.println("preparedStatement ="+preparedStatement);
resultSet = preparedStatement.executeQuery();
List<B> list = new ArrayList<>();
ResultSetMetaData metaData = resultSet.getMetaData();
int counts = metaData.getColumnCount();
while (resultSet.next()){
B b = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
for(int i=0;i<fields.length;i++){
String columnName = metaData.getColumnName(i + 1);
Object object = resultSet.getObject(columnName);
fields[i].setAccessible(true);
fields[i].set(b,object);
}
list.add(b);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
close(connection, preparedStatement,resultSet);
}
return null;
}
// 增删改
public static int update(String sql, Object... args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
// System.out.println(args[i]);
preparedStatement.setObject(i + 1, args[i]);
}
int i = preparedStatement.executeUpdate();
return 1;
} catch (SQLException e) {
e.printStackTrace();
return -1;
} finally {
close(connection, preparedStatement);
}
}
// 关连接
public static void close(Connection conn, PreparedStatement ps) {
try {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println("已关闭");
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println("已关闭");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Connection conn, Statement ps) {
try {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println("已关闭");
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println("已关闭");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void close(Connection conn, Statement ps, ResultSet rs) {
try {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println("已关闭");
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// System.out.println("已关闭");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
session和cookie的使用,使用session显示用户,使用cookie实现记住密码
79 浏览量
2023-12-09
19:01:41
上传
评论
收藏 5.86MB ZIP 举报
小谭の努力
- 粉丝: 110
- 资源: 7
最新资源
- 整卷预览.mhtml
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见
- MySQL是一种广泛使用的开源关系型数据库管理系统,它提供了丰富的SQL语句用于数据库的创建、查询、更新和管理 以下是一些常见的
- 基于Javascript的结婚请帖设计源码 - Invitation
- mysql语句大全及用法
- mysql语句大全及用法
- mysql语句大全及用法
- MySQL是一种广泛使用的开源关系型数据库管理系统
- MySQL是一种广泛使用的开源关系型数据库管理系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈