当前位置:网站首页>Phoenix JDBC
Phoenix JDBC
2022-07-07 20:23:00 【The south wind knows what I mean】
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;
/** * Function is introduced : Use jdbc Operation on Database : Inquire about 、 to update ( Insert / modify / Delete )、 Batch update */
public class DButil {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
/**jdbc Link to */
private Connection conn = null;
/** Get ready sql*/
private PreparedStatement ps = null;
{
initConnection();
}
/** * @param sql * @param params Parameters * Function is introduced : update operation ( modify , Delete , Insert ) */
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 * Function is introduced : Batch update */
public void batchUpdate(String sql, List<Object[]> list) {
if(null == conn){
initConnection();
}
try {
ps = conn.prepareStatement(sql);
// Turn off auto commit transactions
conn.setAutoCommit(false);
// Prevent memory overflow
final int batchSize = 1000;
// Record the number of inserts
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 {
// close resource
closeUpdate();
}
}
/** * @param sql * @param params * Function is introduced : Query operation */
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<>();
// Move the cursor , If the new current line is valid , Then return to true; If there is no next line , Then return to 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 * Function is introduced : Query a record */
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();
// Move the cursor , If the new current line is valid , Then return to true; If there is no next line , Then return to 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));
}
// If there are multiple records , Take the first one .
break;
}
return map;
} catch (Exception e) {
e.printStackTrace();
} finally {
closeQuery(rs);
}
return null;
}
/** * Initialize connection */
private void initConnection() {
try {
//local
conn = DriverManager.getConnection("jdbc:phoenix:192.168.1.220");
} catch (Exception e) {
e.printStackTrace();
}
}
/** * Function is introduced : Turn off updating resources */
private void closeUpdate() {
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/** * @param rs Function is introduced : Close query resources */
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();
}
}
}
边栏推荐
- 4G设备接入EasyGBS平台出现流量消耗异常,是什么原因?
- Measure the height of the building
- You want to kill a port process, but you can't find it in the service list. You can find this process and kill it through the command line to reduce restarting the computer and find the root cause of
- Force buckle 88 Merge two ordered arrays
- [award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
- 备份 TiDB 集群到持久卷
- Nebula importer data import practice
- 【奖励公示】第22期 2022年6月奖励名单公示:社区明星评选 | 新人奖 | 博客同步 | 推荐奖
- PHP method of obtaining image information
- POJ 1742 Coins ( 单调队列解法 )「建议收藏」
猜你喜欢

Opencv learning notes high dynamic range (HDR) imaging
![[MySQL - Basic] transactions](/img/a4/52c4b156b107c1e2f0220b4379eab2.png)
[MySQL - Basic] transactions

VMWare中虚拟机网络配置
![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]

整型int的拼接和拆分

I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!

Vulnhub tre1

Network principle (1) - overview of basic principles

Mongodb learn from simple to deep

Mrs offline data analysis: process OBS data through Flink job
随机推荐
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
Yolov6:yolov6+win10--- train your own dataset
School 1 of vulnhub
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Force buckle 88 Merge two ordered arrays
POJ 1742 Coins ( 单调队列解法 )「建议收藏」
Force buckle 912 Sort array
CodeSonar网络研讨会
Force buckle 1232 Dotted line
4G设备接入EasyGBS平台出现流量消耗异常,是什么原因?
Traversée des procédures stockées Oracle
字符串中数据排序
Force buckle 599 Minimum index sum of two lists
理财产品要怎么选?新手还什么都不懂
kubernetes之创建mysql8
让这个CRMEB单商户微信商城系统火起来,太好用了!
开发一个小程序商城需要多少钱?
恢复持久卷上的备份数据
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
机器学习笔记 - 使用Streamlit探索对象检测数据集