入門 04 - 提供JDBC連接

如果需要的話,您可以自行提供JDBC連接物件給Hibernate使用,而無需透過配置文件設定JDBC來源,一個最簡單的例子如下:
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/HibernateTest?user=root&password=";
java.sql.Connection conn = DriverManager.getConnection(url);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession(conn);

 當然您也可以透過屬性文件hibernate.properties來配置JDBC來源,例如:

hibernate.properties
hibernate.show_sql = true
hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/HibernateTest
hibernate.connection.username = caterpillar
hibernate.connection.password = 123456

 如果是透過XML文件hibernate.cfg.xml則是如下進行配置:

hibernate.cfg.xml
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- 顯示實際操作資料庫時的SQL -->
        <property name="show_sql">true</property>
        <!-- SQL方言,這邊設定的是MySQL -->
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <!-- JDBC驅動程式 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- JDBC URL -->
        <property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
        <!-- 資料庫使用者 -->
        <property name="connection.username">caterpillar</property>
        <!-- 資料庫密碼 -->
        <property name="connection.password">123456</property>

        <!-- 物件與資料庫表格映射文件 -->
        <mapping resource="User.hbm.xml"/>
    </session-factory>

</hibernate-configuration>

 Hibernate在資料庫連接池的使用上是可選的,您可以使用C3P0連接池,當您的屬性文件中含有hibernate.c3p0.*的配置時,就會自動啟用C3P0連接池,而您的CLASSPATH中必須包括c3p0-0.8.4.5.jar,屬性文件hibernate.properties的配置範例如下:

hibernate.properties
hibernate.show_sql = true
hibernate.dialect = net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/HibernateTest
hibernate.connection.username = root
hibernate.connection.password =
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50

 如果是使用hibernate.cfg.xml配置C3P0連接池的例子如下:

hibernate.cfg.xml
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- 顯示實際操作資料庫時的SQL -->
        <property name="show_sql">true</property>
        <!-- SQL方言,這邊設定的是MySQL -->
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <!-- JDBC驅動程式 -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- JDBC URL -->
        <property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>
        <!-- 資料庫使用者 -->
        <property name="connection.username">root</property>
        <!-- 資料庫密碼 -->
        <property name="connection.password"></property>

        <property name="c3p0.min_size">5</property>
        <property name="c3p0.max_size">20</property>
        <property name="c3p0.timeout">1800</property>
        <property name="c3p0.max_statements">50</property>


        <!-- 物件與資料庫表格映射文件 -->
        <mapping resource="User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

 您也可以使用Proxool或DBCP連接池,只要在配置文件中配置hibernate.proxool.或hibernate.dbcp. 等相關選項,這可以在hibernate的etc目錄中找hibernate.properties中的配置例子來參考,當然要記得在CLASSPATH 中加入相關的jar檔案。

如果您使用Tomcat的話,您也可以透過它提供的DBCP連接池來取得連接,您可以先參考這邊的文章來設定Tomcat的DBCP連接池:
DBCP連接池設定

設定好容器提供的DBCP連接池之後,您只要在配置文件中加入connection.datasource屬性,例如在hibernate.cfg.xml中加入:

hibernate.cfg.xml
<property name="connection.datasource">java:comp/env/jdbc/dbname</property>

如果是在hibernate.properties中的話,則加入:

hibernate.properties
hibernate.connection.datasource = java:comp/env/jdbc/dbname
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值