Hibernate入门04 - 提供JDBC连接

来源:互联网 发布:思民挂机软件 编辑:程序博客网 时间:2024/06/05 18:16
 如果需要的话,您可以自行提供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
原创粉丝点击