[javaEE] 開來源資料庫串連池,javaee資料庫連接

來源:互聯網
上載者:User

[javaEE] 開來源資料庫串連池,javaee資料庫連接

一些開源組織提供了資料來源的獨立實現:

DBCP資料庫連接池

C3P0資料庫連接池

Apache Tomcat內建的串連池

 

DBCP串連池

apache提供的串連池實現,需要匯入common-dbcp.jar commons-pool.jar

 

import java.io.FileReader;import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import java.util.Properties;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSourceFactory;public class DBCPTest {    public static void main(String[] args) throws Exception {        //匯入設定檔        Properties prop=new Properties();        prop.load(new FileReader("dbcp.properties"));        //擷取資料來源        BasicDataSourceFactory factory=new BasicDataSourceFactory();        DataSource pool=factory.createDataSource(prop);                Connection conn=pool.getConnection();        //擷取傳輸器對象        Statement statement=conn.createStatement();        //擷取結果集對象        ResultSet resultSet=statement.executeQuery("select * from user");        //遍曆        while(resultSet.next()){            String username=resultSet.getString("username");            System.out.println(username);        }        //關閉資源        resultSet.close();        statement.close();        conn.close();            }}

在項目目錄下建立dacp.properties

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql:///javausername=rootpassword=root

 

C3P0串連池

import java.sql.Connection;import java.sql.ResultSet;import java.sql.Statement;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DBCPTest {    public static void main(String[] args) throws Exception {        //使用C3P0        ComboPooledDataSource pool=new ComboPooledDataSource();        Connection conn=pool.getConnection();        //擷取傳輸器對象        Statement statement=conn.createStatement();        //擷取結果集對象        ResultSet resultSet=statement.executeQuery("select * from user");        //遍曆        while(resultSet.next()){            String username=resultSet.getString("username");            System.out.println(username);        }        //關閉資源        resultSet.close();        statement.close();        conn.close();            }}

C3P0需要在類載入目錄下建立c3p0-config.xml

<?xml version="1.0" encoding="utf-8"?><c3p0-config>    <default-config>        <property name="driverClass">com.mysql.jdbc.Driver</property>        <property name="jdbcUrl">jdbc:mysql:///java</property>        <property name="user">root</property>        <property name="password">root</property>    </default-config></c3p0-config>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.