JDBC连接数据库
mysql版本:
第一种方式:传统方式:使用mysql-connector-java-8.0.11.jar
package CZ.jdbc;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
public class ConnectionTest {
@Test
public void testconnection() throws SQLException {
Driver driver=new com.mysql.cj.jdbc.Driver();
/**
* jdbc:mysql协议
* localhast:IP地址
* 3306:默认MySQL的端口号
* jdbc_learn:数据库
* */
String url="jdbc:mysql://localhost:3306/jdbc_learn?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
//将用户名和密码封装到Properties里
Properties info=new Properties();
info.setProperty("user","root");
info.setProperty("password","1234");
Connection conn= driver.connect(url,info);
System.out.println(conn);
}
}
**第二种方式:**c3p0连接方式:使用c3p0-0.9.1.2.jar
2. 1 c3p0-config.xml
<c3p0-config>
<named-config name="cz">
<!-- 驱动类 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<!-- url-->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_learn?useSSL=false&serverTimezone=UTC</property>
<!-- 用户名 -->
<property name="user">root</property>
<!-- 密码 -->
<property name="password">1234</property>
<!-- 每次增长的连接数-->
<property name="acquireIncrement">5</property>
<!-- 初始的连接数 -->
<property name="initialPoolSize">10</property>
<!-- 最小连接数 -->
<property name="minPoolSize">5</property>
<!-- 最大连接数 -->
<property name="maxPoolSize">10</property>
<!-- 可连接的最多的命令对象数 -->
<property name="maxStatements">5</property>
<!-- 每个连接对象可连接的最多的命令对象数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
2.2 C3p0
package Datasource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import util.JDBCUtils;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class C3p0 {
//1.将c3p0提供c3p0.config.xml拷贝到src目录下
//2.该文件指定了连接数据库和连接吃的相关参数
public void testC3p0_2() throws Exception{
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("cz");
Long start=System.currentTimeMillis();
System.out.println("开始执行");
for (int i=0;i<5000;i++) {
Connection connection = comboPooledDataSource.getConnection();
//System.out.println("连接成功");
connection.close();
}
Long end=System.currentTimeMillis();
System.out.println("c3p0_2连接5000次 耗时="+(end-start));//c3p0连接5000次 耗时=1296
}
}
第三种方式:德鲁伊连接: 使用druid-1.1.10.jar
3.1DruidTest
package Datasource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
public class DruidTest {
@Test
public void TestDruid() throws Exception{
//1.加入 Druid jar包
//2.加入 配置文件,将该文件拷贝到项目的src目录下
//创建 Properties 对象,读取配置文件
InputStream is = DruidTest.class.getClassLoader().getResourceAsStream("druid.properties");
Properties info= new Properties();
info.load(is);
//4.创建一个指定参数的数据库连接池,Druid连接池
DataSource dataSource = DruidDataSourceFactory.createDataSource(info);
Long start=System.currentTimeMillis();
System.out.println("开始执行");
for (int i=0;i<5000;i++) {
Connection connection =dataSource.getConnection();
//System.out.println("连接成功");
connection.close();
}
Long end=System.currentTimeMillis();
System.out.println("德鲁伊连接5000次 耗时="+(end-start));//德鲁伊连接5000次 耗时=1671
}
}
3.2druid.properties
#key=value
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc_learn?useSSL=false&serverTimezone=Asia/Shanghai
username=root
password=1234
#initial connection Size
initialSize=10
#min idle connecton size
minIdle=5
#max active connection size
maxActive=50
#max wait time (5000 mil seconds)
maxWait=5000