package com.neusoft.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.commons.beanutils.BeanUtils;
public class JdbcUtil
{
private static String driver;
private static String url;
private static String user;
private static String password;
/**
*
*静态块
* 在类被第一次加载入内存的时候,执行一次,以后不再执行
*/
static
{
try
{
//获取获取资源文件解析器
ResourceBundle bundle=ResourceBundle.getBundle("jdbc");
//通过解析器获取资源文件中数据填充变量
driver=bundle.getString("DRIVER");
url=bundle.getString("URL");
user=bundle.getString("USER");
password=bundle.getString("PASSWORD");
//3.加载驱动
Class.forName(driver);
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 连接Mysql数据库
*
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception
{
// Properties properties = new Properties();
// InputStream is = JdbcUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
// properties.load(is);
// driver = (properties.getProperty("DRIVER"));
// user = (properties.getProperty("USER"));
// url = (properties.getProperty("URL"));
// password = (properties.getProperty("PASSWORD"));
//
// is.close();
return DriverManager.getConnection(url, user, password);
}
public static long addBatchDate(String sql,int len,Object...obj) throws Exception
{
Connection conn=JdbcUtil.getConnection();
JdbcUtil.beginTransaction(conn);
PreparedStatement pstm=conn.prepareStatement(sql);
long begin=System.currentTimeMillis();
for(int i=0;i<len;i++)
{
for (int j = 0; j < obj.length; j++)
{
pstm.setObject(j+1, obj[j]);
}
pstm.addBatch();
pstm.executeBatch();
pstm.clearBatch();
}
JdbcUtil.commitTransaction(conn);
long end=System.currentTimeMillis();
JdbcUtil.closeResource(conn, pstm, null);
return (end-begin);
}
/**
* 事务回滚
*
* @param conn
*/
public static void rollTransaction(Connection conn)
{
if (conn != null)
{
try
{
conn.rollback();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 事务提交
*
* @param conn
*/
public static void commitTransaction(Connection conn)
{
if (conn != null)
{
try
{
conn.commit();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 事务开启
*
* @param conn
*/
public static void beginTransaction(Connection conn)
{
if (conn != null)
{
try
{
conn.setAutoCommit(false);
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 查一个字段
*
*/
public static <E> E selectvalue(String sql, Object... obj) throws Exception
{
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
conn = JdbcUtil.getConnection();
pstm = conn.prepareStatement(sql);
E value = null;
if (obj != null)
{
for (int i = 0; i < obj.length; i++)
{
pstm.setObject(i + 1, obj[i]);
}
}
rs = pstm.executeQuery();
if (rs.next())
{
value = (E) rs.getObject(1);
}
JdbcUtil.closeResource(conn, pstm, rs);
return value;
}
/**
* Map 的单行查询
*
* @param sql
* @param obj
* @return
* @throws Exception
*/
public static Map<String, Object> selectlow(String sql, Object... obj) throws Exception
{
Connection conn = JdbcUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
Map<String, Object> map = null;
if (obj != null)
{
for (int i = 0; i < obj.length; i++)
{
pstm.setObject(i + 1, obj[i]);
}
}
ResultSet rs = pstm.executeQuery();
ResultSetMetaData rstm = rs.getMetaData();
if (rs.next())
{
int len = rstm.getColumnCount();
int capacity = (int) (len / 0.75 + 1);
map = new HashMap<>(capacity);
for (int i = 0; i < len; i++)
{
String columnName = rstm.getColumnName(i + 1);
Object columnValue = rs.getObject(columnName);
map.put(columnName, columnValue);
}
}
JdbcUtil.closeResource(conn, pstm, rs);
return map;
}
/**
* 利用反射单行查询
*
* @param classObject
* @param sql
* @param obj
* @return
* @throws Exception
*/
public static <T> T selectlow(Class<T> classObject, String sql, Object... obj) throws Exception
{
Connection conn = JdbcUtil.getConnection();
T object = classObject.newInstance();
PreparedStatement pstm = conn.prepareStatement(sql);
if (obj != null)
{
for (int i = 0; i < obj.length; i++)
{
pstm.setObject(i + 1, obj[i]);
}
}
ResultSet rs = pstm.executeQuery();
ResultSetMetaData rstm = rs.getMetaData();
if (rs.next())
{
int len = rstm.getColumnCount();
for (int j = 0; j < len; j++)
{
String columnName = rstm.getColumnName(j + 1);
Object columnValue = rs.getObject(columnName);
BeanUtils.setProperty(object, columnName, columnValue);
}
}
JdbcUtil.closeResource(conn, pstm, rs);
return object;
}
/**
* 利用反射多行查询
*
* @param classObject
* @param sql
* @param obj
* @return
* @throws Exception
*/
public static <T> List<T> selectlows(Class<T> classObject, String sql, Object... obj) throws Exception
{
List<T> list = new ArrayList<>();
Connection conn = JdbcUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
if (obj != null)
{
for (int i = 0; i < obj.length; i++)
{
pstm.setObject(i + 1, obj[i]);
}
}
ResultSet rs = pstm.executeQuery();
ResultSetMetaData rstm = rs.getMetaData();
while (rs.next())
{
T object = classObject.newInstance();
int len = rstm.getColumnCount();
for (int i = 0; i < len; i++)
{
String columnName = rstm.getColumnName(i + 1);
Object columnValue = rs.getObject(columnName);
BeanUtils.setProperty(object, columnName, columnValue);
}
list.add(object);
}
JdbcUtil.closeResource(conn, pstm, rs);
return list;
}
/**
* 利用Map的多行查询
*
* @param sql
* @param obj
* @return
* @throws Exception
*/
public static List<Map<String, Object>> selectlows(String sql, Object... obj) throws Exception
{
Connection conn = JdbcUtil.getConnection();
PreparedStatement pstm = conn.prepareStatement(sql);
Map<String, Object> map = null;
List<Map<String, Object>> list = new ArrayList<>();
if (obj != null)
{
for (int i = 0; i < obj.length; i++)
{
pstm.setObject(i + 1, obj[i]);
}
}
ResultSet rs = pstm.executeQuery();
ResultSetMetaData rstm = rs.getMetaData();
while (rs.next())
{
int len = rstm.getColumnCount();
int capity = (int) (len / 0.75 + 1);
map = new HashMap<>(capity);
for (int i = 0; i < len; i++)
{
String columnName = rstm.getColumnName(i + 1);
Object columnValue = rs.getObject(columnName);
map.put(columnName, columnValue);
}
list.add(map);
}
JdbcUtil.closeResource(conn, pstm, rs);
return list;
}
/**
* 数据增删改
*
* @param sql
* @param obj
* @return
* @throws Exception
*/
public static int up
小白求带528
- 粉丝: 63
- 资源: 2
最新资源
- 基于dubbo-go、gin的集成项目资料齐全+详细文档.zip
- 基于dubbo2.5.3开发的监控平台,兼容了dubbo-admin的特性,有redis、mysql两个版本资料齐全+详细文档.zip
- 基于Dubbo的agent探针数据采集模块资料齐全+详细文档.zip
- 基于Dubbo-RPC的分布式配置服务中心资料齐全+详细文档.zip
- 基于dubbo的分布式工程开发规范实例工程,分布式跟踪、ID生成、分布式事务、分布式治理、分表分库、分布式锁、选举、分布式配置、API文档生成器...资料齐全+详细文档.zip
- 基于Dubbo的分布式任务调度系统资料齐全+详细文档.zip
- 基于dubbo的分布式商城资料齐全+详细文档.zip
- 基于dubbo的分布式数据库事务资料齐全+详细文档.zip
- @Transactional事务,太坑了!前言 对于从事java开发工作的同学来说,Spring的事务肯定再熟悉不过了
- 基于dubbo的微服务架构资料齐全+详细文档.zip
- 基于dubbo第三方支付系统资料齐全+详细文档.zip
- 基于Dubbo框架的raft算法库资料齐全+详细文档.zip
- 基于dubbo框架+leveldb存储构建高可用的事件通讯组件资料齐全+详细文档.zip
- 基于Dubbo分布式简易支付系统资料齐全+详细文档.zip
- 基于Dubbo埋点的分布式调用跟踪系统资料齐全+详细文档.zip
- 基于Dubbo微服务项目快速搭建脚手架,提供基础功能,方便企业快速搭建项目。资料齐全+详细文档.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈