当前位置:网站首页>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();
}
}
}
边栏推荐
- JVM 类加载机制
- Kubernetes——kubectl命令行工具用法详解
- MSE API learning
- Force buckle 1961 Check whether the string is an array prefix
- Cloud component development and upgrading
- 力扣 989. 数组形式的整数加法
- Vulnhub's funfox2
- Equals method
- pom.xml 配置文件标签作用简述
- Data island is the first danger encountered by enterprises in their digital transformation
猜你喜欢
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!
Force buckle 599 Minimum index sum of two lists
One click deployment of any version of redis
Splicing and splitting of integer ints
Sword finger offer II 013 Sum of two-dimensional submatrix
力扣 2319. 判断矩阵是否是一个 X 矩阵
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
ASP.NET学习& asp‘s one word
php 获取图片信息的方法
随机推荐
Sword finger offer II 013 Sum of two-dimensional submatrix
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Oracle 存储过程之遍历
Force buckle 599 Minimum index sum of two lists
Boot 和 Cloud 的版本选型
Opencv学习笔记 高动态范围 (HDR) 成像
【哲思与实战】程序设计之道
Creation of kubernetes mysql8
pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
Mrs offline data analysis: process OBS data through Flink job
【解决】package ‘xxxx‘ is not in GOROOT
gorilla官方:golang开websocket client的示例代码
力扣 599. 两个列表的最小索引总和
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
解决/bin/sh进去的容器运行可执行文件报not found的问题
About cv2 dnn. Readnetfromonnx (path) reports error during processing node with 3 inputs and 1 outputs [exclusive release]
831. KMP string
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
Solve the problem of incomplete display around LCD display of rk3128 projector
Jenkins 用户权限管理