代码:
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/teach?user=root&password=root";
java.sql.Connection conn = DriverManager.getConnection(url);
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession(conn);
当然您也可以通过属性文件hibernate.properties来配置JDBC来源,例如:
代码:
hibernate.show_sql = true
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://127.0.0.1:3306/teach
hibernate.connection.username = root
hibernate.connection.password = root
如果是通过XML文件hibernate.cfg.xml则是如下进行配置:
代码:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,这儿设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/teach</property>
<!-- 数据库使用者 -->
<property name="connection.username">root</property>
<!-- 数据库密码 -->
<property name="connection.password">root</property>
<!-- 对象与数据库表映射文件 -->
<mapping resource="com/xtedu/teach/hibernate/mappings/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate在数据库连接池的使用上是可选的,您可以使用C3P0连接池,当您的属性文件中含有hibernate.c3p0.*的配置时,就会自动启用C3P0连接池,而您的CLASSPATH中必须包括c3p0-0.8.4.5.jar,属性文件hibernate.properties的配置范例如下:
代码:
hibernate.show_sql = true
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://127.0.0.1:3306/teach
hibernate.connection.username = root
hibernate.connection.password = root
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
如果是使用hibernate.cfg.xml配置C3P0连接池的例子如下:
代码:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 显示实际操作数据库时的SQL -->
<property name="show_sql">true</property>
<!-- SQL方言,这儿设定的是MySQL -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- JDBC驱动程序 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/teach</property>
<!-- 数据库使用者 -->
<property name="connection.username">root</property>
<!-- 数据库密码 -->
<property name="connection.password">root</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="com/xtedu/teach/hibernate/mappings/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
您也可以使用Proxool或DBCP连接池,只要在配置文件中配置hibernate.proxool.*或hibernate.dbcp.*等相关选项,这可以在hibernate的etc目录中找hibernate.properties中的配置例子来参考,当然要记得在CLASSPATH中加入相关的jar档案。
如果您使用Tomcat的话,您也可以通过它提供的DBCP连接池来获取连接,您可以先参考这儿的文章来设定Tomcat的DBCP连接池:
http://www.caterpillar.onlyfun.net/phpBB2/viewtopic.php?t=1354
设定好容器提供的DBCP连接池之后,您只要在配置文件中加入connection.datasource属性,例如在hibernate.cfg.xml中加入:
代码:
<property name="connection.datasource">java:comp/env/jdbc/dbname</property>
如果是在hibernate.properties中的话,则加入:
代码:
hibernate.connection.datasource = java:comp/env/jdbc/dbname