当前位置:网站首页>Phoenix JDBC
Phoenix JDBC
2022-07-07 18:15:00 【南风知我意丿】
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * 功能介绍:使用jdbc对数据库操作:查询、更新(插入/修改/删除)、批量更新 */
public class DButil {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
/**jdbc的链接*/
private Connection conn = null;
/**准备sql*/
private PreparedStatement ps = null;
{
initConnection();
}
/** * @param sql * @param params 参数 * 功能介绍:更新操作(修改,删除,插入) */
public int executeUpdate(String sql, Object[] params) {
if(null == conn){
initConnection();
}
try {
ps = conn.prepareStatement(sql);
if (params.length != 0) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
int rows = ps.executeUpdate();
conn.commit();
return rows;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeUpdate();
}
return 0;
}
/** * @param sql * @param list * 功能介绍:批量更新 */
public void batchUpdate(String sql, List<Object[]> list) {
if(null == conn){
initConnection();
}
try {
ps = conn.prepareStatement(sql);
//关闭自动提交事务
conn.setAutoCommit(false);
//防止内存溢出
final int batchSize = 1000;
//记录插入数量
int count = 0;
int size = list.size();
Object[] obj = null;
for (int i = 0; i < size; i++) {
obj = list.get(i);
for (int j = 0; j < obj.length; j++) {
ps.setObject(j + 1, obj[j]);
}
ps.addBatch();
if (++count % batchSize == 0) {
ps.executeBatch();
conn.commit();
}
}
ps.executeBatch();
conn.commit();
conn.setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
try {
conn.rollback();
conn.setAutoCommit(true);
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally {
//关闭资源
closeUpdate();
}
}
/** * @param sql * @param params * 功能介绍:查询操作 */
public List<Map<String, Object>> executeQuery(String sql, Object[] params) {
if(null == conn){
initConnection();
}
ResultSet rs = null;
List<Map<String, Object>> list = null;
try {
ps = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
long startTime = System.currentTimeMillis();
rs = ps.executeQuery();
LOGGER.info("UserBigTableService sql-executeQuery-time: " + (System.currentTimeMillis() - startTime) + "ms");
list = new ArrayList<>();
//移动光标,如果新的当前行有效,则返回 true;如果不存在下一行,则返回 false
while (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
Map<String, Object> map = new HashMap<>(16);
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
list.add(map);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeQuery(rs);
}
return null;
}
/** * @param sql * @param params * 功能介绍:查询操作一条记录 */
public Map<String, Object> query (String sql, Object[] params) {
if(null == conn){
initConnection();
}
ResultSet rs = null;
Map<String, Object> map = null;
try {
ps = conn.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
ps.setObject(i + 1, params[i]);
}
}
rs = ps.executeQuery();
//移动光标,如果新的当前行有效,则返回 true;如果不存在下一行,则返回 false
while (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
map = new HashMap<>(16);
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
map.put(rsmd.getColumnName(i), rs.getObject(i));
}
//若有多条记录,取第一条。
break;
}
return map;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeQuery(rs);
}
return null;
}
/** * 初始化连接 */
private void initConnection() {
try {
//local
conn = DriverManager.getConnection("jdbc:phoenix:192.168.1.220");
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 功能介绍:关闭更新资源 */
private void closeUpdate() {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/** * @param rs 功能介绍:关闭查询资源 */
private void closeQuery(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
边栏推荐
- POJ 1742 coins (monotone queue solution) [suggestions collection]
- Traversal of Oracle stored procedures
- vulnhub之tre1
- JVM GC garbage collection brief
- The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared
- pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
- 【mysql篇-基础篇】事务
- Opencv学习笔记 高动态范围 (HDR) 成像
- 力扣 88.合并两个有序数组
- 关于cv2.dnn.readNetFromONNX(path)就报ERROR during processing node with 3 inputs and 1 outputs的解决过程【独家发布】
猜你喜欢

How to test CIS chip?

机器学习笔记 - 使用Streamlit探索对象检测数据集

Sword finger offer II 013 Sum of two-dimensional submatrix

Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"

Some important knowledge of MySQL

让这个CRMEB单商户微信商城系统火起来,太好用了!

Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!
![[MySQL - Basic] transactions](/img/a4/52c4b156b107c1e2f0220b4379eab2.png)
[MySQL - Basic] transactions

力扣 599. 两个列表的最小索引总和

ASP. Net learning & ASP's one word
随机推荐
[solution] package 'XXXX' is not in goroot
Opencv学习笔记 高动态范围 (HDR) 成像
搞定带WebKitFormBoundary post登录
SQL common optimization
开发一个小程序商城需要多少钱?
Sword finger offer II 013 Sum of two-dimensional submatrix
机器学习笔记 - 使用Streamlit探索对象检测数据集
力扣 459. 重复的子字符串
Cuda版本不一致,编译apex报错
MRS离线数据分析:通过Flink作业处理OBS数据
Force buckle 1961 Check whether the string is an array prefix
4G设备接入EasyGBS平台出现流量消耗异常,是什么原因?
一键部署Redis任意版本
POJ 1742 coins (monotone queue solution) [suggestions collection]
Creation of kubernetes mysql8
Vulnhub's funfox2
力扣 599. 两个列表的最小索引总和
浅尝不辄止系列之试试腾讯云的TUIRoom(晚上有约,未完待续...)
【解决】package ‘xxxx‘ is not in GOROOT
【mysql篇-基础篇】事务