当前位置:网站首页>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();
}
}
}
边栏推荐
- 一. 基础概念
- With st7008, the Bluetooth test is completely grasped
- 力扣 1790. 仅执行一次字符串交换能否使两个字符串相等
- 【解决】package ‘xxxx‘ is not in GOROOT
- CSDN语法说明
- Traversal of Oracle stored procedures
- Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
- YoloV6:YoloV6+Win10---训练自己得数据集
- Force buckle 989 Integer addition in array form
- 力扣 1037.有效的回旋镖
猜你喜欢
![About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]](/img/59/33381b8d45401607736f05907ee381.png)
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]

【STL】vector

Opencv学习笔记 高动态范围 (HDR) 成像

PHP method of obtaining image information

Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!

vulnhub之Funfox2
![[MySQL - Basic] transactions](/img/a4/52c4b156b107c1e2f0220b4379eab2.png)
[MySQL - Basic] transactions

Opencv learning notes high dynamic range (HDR) imaging

The state cyberspace Office released the measures for data exit security assessment: 100000 information provided overseas needs to be declared

Some important knowledge of MySQL
随机推荐
力扣 599. 两个列表的最小索引总和
Traversée des procédures stockées Oracle
力扣599. 两个列表的最小索引总和
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
Precautions for cjson memory leakage
MRS离线数据分析:通过Flink作业处理OBS数据
Force buckle 1961 Check whether the string is an array prefix
ASP. Net learning & ASP's one word
Force buckle 2319 Judge whether the matrix is an X matrix
Cuda版本不一致,编译apex报错
How to test CIS chip?
Micro service remote debug, nocalhost + rainbow micro service development second bullet
TS快速入门-泛型
Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!
整型int的拼接和拆分
力扣 643. 子数组最大平均数 I
Force buckle 912 Sort array
开发一个小程序商城需要多少钱?
解决/bin/sh进去的容器运行可执行文件报not found的问题
pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别