dbcp_c3p0连接mysql8.0.13
生活随笔
收集整理的這篇文章主要介紹了
dbcp_c3p0连接mysql8.0.13
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
背景
學習數據庫的使用,上次沒有記錄,現在都回憶不起來了,所以這次重新學的時候順便記錄下。
配置環境
- 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
使用代碼連接數據庫
代碼
測試
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
使用代碼連接數據庫
代碼
曾經報錯
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 {@Testpublic 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();} }轉載于:https://www.cnblogs.com/richardwlee/p/10308507.html
總結
以上是生活随笔為你收集整理的dbcp_c3p0连接mysql8.0.13的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL总论
- 下一篇: linux cmake编译源码,linu