当前位置:网站首页>Druid -- JDBC tool class case
Druid -- JDBC tool class case
2022-04-23 04:40:00 【Z-hhhhh】
What is? Druid?
Druid Is an efficient data query system , The main solution is to aggregate query a large number of time-based data . Data can be ingested in real time , Enter into Druid It can be checked immediately after , At the same time, the data is almost immutable . It's usually a matter of fact event based on timing , After the fact comes into Druid, The external system can query the fact . At present, the commonly used data sources are c3p0、dbcp、proxool、druid.
Druid characteristic :
Sub second query :druid It provides fast aggregation ability and sub second performance OLAP Query power , Multi tenant design , It's an ideal way to analyze applications for users
Real time data injection :druid Support stream data injection , And provides event driven data , Ensure the effectiveness and unity of events in real-time and offline environments
Extensible PB Levels of storage :druid Clusters can be easily expanded to PB The amount of data , A million levels of data injection per second . Even if you scale up the data , It can also guarantee its effectiveness
Multi environment deployment :druid It can run on commercial hardware , It can also run on the cloud . It can inject data from a variety of data systems , Include hadoop,spark,kafka,storm and samza etc.
A rich community :druid Have a rich community , For everyone to learn
rely on
<properties>
<mysql.version>5.1.49</mysql.version>
<log4j.version>1.2.12</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.27</version>
</dependency>
<!-- commons-dbutils -->
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
</dependencies>
It needs to be modified according to the situation database,username,password
jdbc.properties The configuration file
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.71.222:3306/test?useSSL=false&createDatabaseIfNotExist=true&characterEncoding=UTF-8
username=root
password=root
** The following configuration is not required
* Initialization length
initialSize=5
* maximum connection
maxActive=10
* Timeout waiting time
maxWait=3000
log4j.properties file
log4j.rootLogger=warn, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
Prepare a table
create databases test;
use test;
create table student(
name varchar(30),
age int(10),
gender varchar(10)
);
insert into student(name,age,gender) values('zwh',19,'male');
insert into student(name,age,gender) values('cm',18,'female');
// Check it out
select * from stundet;
JDBCReadUtils
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class JDBCReadUtils implements Serializable {
private static Logger logger = Logger.getLogger(JDBCReadUtils.class);
/** * Realization JDBCHelper Singleton of */
private static JDBCReadUtils instance = null;
private QueryRunner runner = null;
/** * In the process of implementing the singleton , Create a unique database connection pool */
private JDBCReadUtils(String url) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(new File(url)));
runner = new QueryRunner(DruidDataSourceFactory.createDataSource(properties));
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
public JDBCReadUtils() {
}
/** * Access to the singleton */
public static JDBCReadUtils getInstance(String url) {
if (instance == null) {
synchronized (JDBCReadUtils.class) {
if (instance == null) {
instance = new JDBCReadUtils(url);
}
}
}
return instance;
}
/** * Inquire about ( return Array result ) */
private Object[] queryArray(String sql, Object... params) {
Object[] result = null;
try {
result = runner.query(sql, new ArrayHandler(), params);
} catch (SQLException e) {
logger.error(e.getMessage());
}
return result;
}
/** * Inquire about ( return ArrayList result ) */
public List<Object[]> queryArrayList(String sql, Object... params) {
List<Object[]> result = null;
try {
result = runner.query(sql, new ArrayListHandler(), params);
} catch (SQLException e) {
logger.error(e.getMessage());
}
return result == null ? new ArrayList<Object[]>() : result;
}
/** * to update ( Include UPDATE、INSERT、DELETE, Returns the number of affected rows ) */
public int update(String sql, Object... params) {
int result = 0;
try {
result = runner.update(sql, params);
} catch (SQLException e) {
logger.error(e.getMessage());
}
return result;
}
}
JDBCTest
import java.util.List;
public class JDBCTest {
public static void main(String[] args) {
JDBCReadUtils readUtils = JDBCReadUtils
.getInstance("D:\\javaproject\\k12project\\druidJDBC\\src\\main\\resources\\jdbc.properties");
// lookup
//o[0] Write according to the number of fields , The following dynamic parameters are used to write where Conditions
// Such as :List<Object[]> result = readUtils.queryArrayList("select * from student where age>?","18");
/*List<Object[]> result = readUtils.queryArrayList("select name,age from student"); for (Object[] o : result) { System.out.println(o[0]+ "\t" + o[1] ); }*/
// to update
int update = readUtils.update("insert into student(name,age,gender) values('wjing',12,'female') ");
System.out.println(update);
}
}
版权声明
本文为[Z-hhhhh]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220559122754.html
边栏推荐
- Installation and use of Apache bench (AB pressure test tool)
- 无线键盘全国产化电子元件推荐方案
- Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
- [AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
- 华为机试--高精度整数加法
- QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
- 520. Detect capital letters
- Coinbase:关于跨链桥的基础知识、事实和统计数据
- Interaction of diet gut microbiota on cardiovascular disease
- 1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
猜你喜欢
Go反射法则
MYSQL去重方法汇总
Coinbase: basic knowledge, facts and statistics about cross chain bridge
Recommended scheme of national manufactured electronic components
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Mysql50 basic exercises
C语言: 指针的进阶
Key points of AWS eks deployment and differences between console and eksctl creation
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
zynq平台交叉编译器的安装
随机推荐
Migrate from MySQL database to AWS dynamodb
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
Leetcode001 -- returns the subscript of the array element whose sum is target
Leetcode009 -- search the target value in the array with binary search
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
Go 语言中的 logger 和 zap 日志库
Supplement 14: cmake practice project notes (to be continued 4 / 22)
What is the thirty-six plan
Leetcode008 -- implement strstr() function
leetcode005--原地删除数组中的重复元素
test
Leetcode - > 1 sum of two numbers
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
[paper reading] [3D object detection] voxel transformer for 3D object detection
Recommended scheme for national production of electronic components of wireless keyboard
Summary of MySQL de duplication methods
IEEE Transactions on Industrial Informatics(TII)投稿须知
Small volume Schottky diode compatible with nsr20f30nxt5g
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
shell wc (统计字符数量)的基本使用