11、JDBC-Druid
依赖
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>yofc</groupId>
<artifactId>jdbc</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
</dependencies> <build>
<plugins>
<!-- 指定jdk -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
获取连接
无配置文件
@Test
public void testDruid() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null; DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://192.168.8.136:3306/jdbc");
dataSource.setUsername("root");
dataSource.setPassword("root");
try {
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("select * from user");
rset = pstmt.executeQuery();
while (rset.next()) {
System.out.println(rset.getInt("id") + " " + rset.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rset != null) rset.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
}
}
}
有配置文件
druid.properties
username=root
password=root
url=jdbc:mysql://192.168.8.136:3306/jdbc
driverClassName=com.mysql.cj.jdbc.Driver
@Test
public void testDruidWithConfig() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rset = null; try {
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("select * from user");
rset = pstmt.executeQuery();
while (rset.next()) {
System.out.println(rset.getInt("id") + " " + rset.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rset != null) rset.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (Exception e) {
}
}
}
官方文档
11、JDBC-Druid的更多相关文章
- 【Spring】11、Spring事务管理
写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: ...
- Spring入门(三)— AOP注解、jdbc模板、事务
一.AOP注解开发 导入jar包 aop联盟包. aspectJ实现包 . spring-aop-xxx.jar . spring-aspect-xxx.jar 导入约束 aop约束 托管扩展类和被扩 ...
- 40、JDBC相关概念介绍
1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡,同样道理,我们安装好数据 ...
- mybatis基本流程、jdbc连接、ps:附mybatis(乐观锁)实现
一.前言 Mybatis和Hibernate一样,是一个优秀的持久层框架.已经说过很多次了,原生的jdbc操作存在大量的重复性代码(如注册驱动,创建连接,创建statement,结果集检测等).框架的 ...
- 已看1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架、多线程(并发编程)、I/O(NIO)、Socket、JDBC、XML、反射等。[泛型]\
1.熟练的使用Java语言进行面向对象程序设计,有良好的编程习惯,熟悉常用的Java API,包括集合框架.多线程(并发编程).I/O(NIO).Socket.JDBC.XML.反射等.[泛型]\1* ...
- Hadoop Hive概念学习系列之hive里的扩展接口(CLI、Beeline、JDBC)(十六)
<Spark最佳实战 陈欢>写的这本书,关于此知识点,非常好,在94页. hive里的扩展接口,主要包括CLI(控制命令行接口).Beeline和JDBC等方式访问Hive. CLI和B ...
- java语言体系的技术简介之JSP、Servlet、JDBC、JavaBean(Application)
转自:https://zhangkunnan.iteye.com/blog/2040462 前言 Java语言 Java语言体系比较庞大,包括多个模块.从WEB项目应用角度讲有JSP.Servlet. ...
- 小白的springboot之路(五)、集成druid
0-前言 Druid阿里巴巴开源的一个java数据库连接池,是Java语言中最好的数据库连接池,Druid能够提供强大的监控和扩展功能:集成它能够方便我们对数据库连接进行监控和分析,下面我们来集成它: ...
- ABP(现代ASP.NET样板开发框架)系列之11、ABP领域层——仓储(Repositories)
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之11.ABP领域层——仓储(Repositories) ABP是“ASP.NET Boilerplate Proj ...
- WebStorm 11、PhpStorm 10免费激活(不需要注册码)
之前分享的WebStorm9.WebStrom10.PhpStorm9注册码生成网站已经被站长关了,比较遗憾!http://my.oschina.net/ximidao/blog/486353 现在官 ...
随机推荐
- 洛谷P1916 小书童——蚂蚁大战
题目背景 小A在你的帮助下,开始“刷题”,他在小书童里发现了一款叫“蚂蚁大战”(又称蛋糕保卫战)的游戏.(你懂得) 题目描述 游戏中会出现n只蚂蚁,分别有a1,a2……an的血量,它们要吃你的蛋糕.当 ...
- CodeForces615B-Longtail Hedgehog-dp/图
记忆化数组记录从这个点的最长下降序列,然后乘以这个点的度,就是ans,维护即可. #include <cstdio> #include <cstring> #include & ...
- Visible Trees HDU - 2841
Visible Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- 使用gradle命令代替CUBA Studio,启动项目
在cuba platform开发中,一开始都会使用CUBA Studio,这是一个脚手架,可以很方便的创建数据表.视图.Bean等.但是有时也会有奇怪的问题,比如不能读取本地maven 仓库,只读取远 ...
- windows 设置ipsec防火墙
windows server 推荐使用ipsec修改防火墙设置,默认防火墙需要手动导入导出.wfw文件,需要手动添加单条规则,维护麻烦,推荐关闭,使用ipsec管理 以下是线上防火墙配置,可参照业务环 ...
- 关于360插件化Replugin Activity动态修改父类的字节码操作
近期在接入360插件化方案Replugin时,发现出现崩溃情况. 大概崩溃内容如下: aused by: java.lang.ClassNotFoundException: Didn't find c ...
- 【转】使用STM32F4的CCM内存
我们知道STM32F4当中有个CCM内存,如图所示,这个内存是挂在D总线上直接和内核相连,因此除了内核之外谁都不能访问,那么我们怎么将其利用起来呢?网上这个资料还真的很少,今天我就给大家分享一下,献给 ...
- python中,下级模块引用上级模块:SystemError: Parent module '' not loaded, cannot perform relative import
当在下级中引用上级时,使用相对导包会出错,SystemError: Parent module '' not loaded, cannot perform relative import 运行test ...
- os模块总结
学了忘,忘了学,忘了就来看一下...唯一进步的就是这次学的比上次更快了- - 最常用的几个: os.getcwd() # os.path.abspath(os.path.dirname(__fil ...
- kvm虚拟化管理
虚拟化 KVM (kernel-based virtual machine) 常见的一些虚拟化的软件xen kvm vmware esx openVZ Oracle VM VirtualBox vsp ...