分数太少了,稍微提一下:
如果需要的話,您可以自行提供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.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則是如下進行配置:
代碼:
<?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.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連接池的例子如下:
代碼:
<?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檔案
如果需要的話,您可以自行提供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.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則是如下進行配置:
代碼:
<?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.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連接池的例子如下:
代碼:
<?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檔案
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货