日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

dbcp_c3p0连接mysql8.0.13

發布時間:2024/4/17 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

    使用代碼連接數據庫

    代碼

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 {@Testpublic 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 ,名字和地址都不能改

配置文件代碼,注意其中的 & 要轉義為&amp

<c3p0-config><default-config><!-- 必要參數 --><property name="driverClass">com.mysql.cj.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost:3306/webdemo?useSSL=false&amp;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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。