背景
學習數據庫的使用,上次沒有記錄,現在都回憶不起來了,所以這次重新學的時候順便記錄下。
配置環境
- win10
- jdk11
- idea
- mysql8.0.13
DBCP連接使用
用配置文件目前我連接不來
jar包
- mysql-connector-java-8.0.14
- commons-pool2-2.6.0
- commons-logging-1.2
- commons-dbcp2-2.5.0
使用代碼連接數據庫
代碼
import org.apache.commons.dbcp2.BasicDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
BasicDataSource dataSource = new BasicDataSource();
/*mysql數據庫的連接,參考我上篇文章*/
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/webdemo?useSSL=false&serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("root");
測試
Connection conn = dataSource.getConnection();
String sql = "INSERT INTO category VALUES('ee','ee');";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
/*增刪改:執行更新*/
System.out.println(preparedStatement.executeUpdate());
曾經報錯
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
解決方法:導入commons-logging.jar
C3P0的使用
jar包
- c3p0-0.9.5.2
- mchange-commons-java-0.2.11
- mysql-connector-java-8.0.14
使用代碼連接數據庫
代碼
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class c3p0Demo {
@Test
public void c3p0Test() throws Exception {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/webdemo?useSSL=false&serverTimezone=UTC");
dataSource.setUser("root");
dataSource.setPassword("root");
Connection conn = dataSource.getConnection();
String sql = "INSERT INTO category VALUES('bvb','gg');";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
}
}
曾經報錯
java.lang.NoClassDefFoundError:com.mchange.v2.ser.Indirector
解決方法:這是c3p0的一個錯誤信息,我們在下載 c3p0時候,zip壓縮包中,有三個jar,其中一個 c3p0-x.x.x.jar,還有一個 mchange.......jar的文件,導入即可
使用配置文件連接數據庫
在src文件夾下創建 c3p0-config.xml ,名字和地址都不能改
配置文件代碼,注意其中的 & 要轉義為&
<c3p0-config>
<default-config>
<!-- 必要參數 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/webdemo?useSSL=false&serverTimezone=UTC</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 下面不是必要的參數 -->
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
</c3p0-config>
測試代碼
package cn.wahll.test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class c3p0Demo {
@Test
public void c3p0PoolTest() throws Exception {
//直接找到配置文件下的默認配置
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//測試代碼
Connection conn = dataSource.getConnection();
String sql = "INSERT INTO category VALUES('bsafvb','asdgg')";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();
}
}