mysql数据库,Maven 构建
1.c3p0的连接
pom.xml 注意导的包 c3p0 有两个包(都要有)
com.mchange
c3p0
0.9.5.5
mysql
mysql-connector-java
5.1.42
第一种方式:(不要配置文件)
public static void main(String[] args) throws SQLException, PropertyVetoException {
ComboPooledDataSource sc=new ComboPooledDataSource();
sc.setDriverClass("com.mysql.jdbc.Driver");
sc.setJdbcUrl("jdbc:mysql://localhost:3306/test");
sc.setUser("root");
sc.setPassword("root");
sc.setInitialPoolSize(10);
Connection conn=sc.getConnection();
System.out.println(conn);
}
第二种方式(通过配置文件)
配置文件名 c3p0-config.xml
com.mysql.jdbc.Driver
jdbc:mysql:///test
root
root
5
10
10
100
50
2
java测试代码
public static void main(String[] args) throws SQLException {
ComboPooledDataSource sc=new ComboPooledDataSource("hellc3p0");
Connection conn=sc.getConnection();
System.out.println(conn);
}
2.DBCP
pom.xml
commons-dbcp
commons-dbcp
1.4
mysql
mysql-connector-java
5.1.42
第一种方式
BasicDataSource source =new BasicDataSource();
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl("jdbc:mysql:///test");
source.setUsername("root");
source.setPassword("root");
source.setInitialSize(10);
source.setMaxActive(10);
Connection conn=source.getConnection();
System.out.println(conn);
第二种方式 配置文件
文件名 dbcp.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=root
initialSize=10
测试代码
Properties pros = new Properties();
//方式1:
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
//方式2:
// FileInputStream is = new FileInputStream(new File("src\\main\\resources\\dbcp.properties"));
pros.load(is);
DataSource source = BasicDataSourceFactory.createDataSource(pros);
Connection conn = source.getConnection();
System.out.println(conn);
3.druid
pom.xml
com.alibaba
druid
1.1.23
mysql
mysql-connector-java
5.1.42
druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=root
initialSize=10
maxActive=10
测试代码
public static void main(String[] args) throws Exception {
DataSource source1;
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
pros.load(is);
source1 = DruidDataSourceFactory.createDataSource(pros);
Connection conn = source1.getConnection();
System.out.println(conn);
}
4.HikariCP
导入依赖pom.xml
com.zaxxer
HikariCP
3.4.5
mysql
mysql-connector-java
5.1.42
第一种方式:
public static void main(String[] args) throws SQLException {
//实例化类
HikariConfig hikariConfig = new HikariConfig();
//设置url
hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8");
//数据库帐号
hikariConfig.setUsername("root");
//数据库密码
hikariConfig.setPassword("root");
hikariConfig.addDataSourceProperty("cachePrepStmts", "true");
hikariConfig.addDataSourceProperty("prepStmtCacheSize", "250");
hikariConfig.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
HikariDataSource ds = new HikariDataSource(hikariConfig);
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
System.out.println(conn);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
第二种 方式 通过配置文件
HiKari.properties 只要要这三个就可以了,连接
jdbcUrl=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
username=root
password=root
测试代码:
public static void main(String[] args) throws IOException, SQLException {
Properties pros = new Properties();
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("Hikari.properties");
pros.load(is);
HikariConfig hikariConfig = new HikariConfig(pros);
HikariDataSource ds = new HikariDataSource(hikariConfig);
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = ds.getConnection();
System.out.println(conn);
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}