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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java----连接池C3p0使用的补充

發(fā)布時(shí)間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java----连接池C3p0使用的补充 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C3p0數(shù)據(jù)庫的連接方式是目前市場場最為廣泛的類型之一

本篇主要你演示C3p0使用文件配置和不使用文件配置的兩種操作方式

#######使用文件配置

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException;import com.dbutil.zyz.ConnLink; import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Demo1 { //首先演示下C3p0不使用配置文件的連接數(shù)據(jù)庫的方式static ConnLink connlink=new ConnLink();static Connection conn=null;static PreparedStatement pstmt=null;public static void main(String[] args) throws SQLException {try {//總體步驟配置如下,可以作為模板使用//1.創(chuàng)建datasourceComboPooledDataSource dataSource=new ComboPooledDataSource();//2.設(shè)置連接數(shù)據(jù)的信息dataSource.setJdbcUrl("jdbc:mysql://localhost/test");dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setUser("root");dataSource.setPassword("root");//得到連接對(duì)象conn=dataSource.getConnection();String sql="insert into blank values(null,?,?)";pstmt=conn.prepareStatement(sql);pstmt.setString(1, "test");pstmt.setInt(2,2000);pstmt.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally{connlink.relese(conn, pstmt);}} }

?

########不使用文件配置

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Demo2 {//下面開始演示使用配置文件的C3p0用法static ConnLink connlink=new ConnLink();static Connection conn=null;static PreparedStatement pstmt=null;public static void main(String[] args) throws SQLException {//就new了一個(gè)對(duì)象。在這種情況下c3p0會(huì)直接找到c3p0-config.xml文件//并且在c3p0-config.xml文件中默認(rèn)的找到 default-config配置try {ComboPooledDataSource dataSource=new ComboPooledDataSource();//2.得到連接對(duì)象conn=dataSource.getConnection();String sql="insert into blank values(null,?,?)";pstmt=conn.prepareStatement(sql);pstmt.setString(1, "root");pstmt.setInt(2,20000);pstmt.executeUpdate();} catch (Exception e) {e.printStackTrace();}finally{connlink.relese(conn, pstmt);}} }

?

其中配置文件的信息如下(文件名稱不可以更改)

<?xml version="1.0" encoding="UTF-8"?> <c3p0-config><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost/test</property><property name="user">root</property><property name="password">root</property><property name="initialPoolSize">5</property><property name="maxPoolSize">20</property></default-config><named-config name="oracle"> <property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql:///web_07</property><property name="user">root</property><property name="password">123</property></named-config></c3p0-config>

?

注意:代碼中的釋放調(diào)用代碼如下

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;public class ConnLink { //數(shù)據(jù)庫的連接部分public String jdbc_driver="com.mysql.jdbc.Driver";public String jdbc_conn="jdbc:mysql://localhost:3306/test";public String user="root";public String pass="root";//返回連接函數(shù)的部分public Connection getConn() throws SQLException, ClassNotFoundException{//1.注冊驅(qū)動(dòng) Class.forName(jdbc_driver);//2.獲取連接Connection connection=DriverManager.getConnection(jdbc_conn,user,pass);return connection;}//釋放連接資源的部分public void relese(Connection conn,PreparedStatement pstmt) throws SQLException{if(pstmt!=null) pstmt.close();if(conn!=null) conn.close();} }

?

?

#####數(shù)據(jù)庫變化如下

?

轉(zhuǎn)載于:https://www.cnblogs.com/byczyz/p/11349052.html

總結(jié)

以上是生活随笔為你收集整理的java----连接池C3p0使用的补充的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。