Getting Started 04 - Provide JDBC connection
If you need it, you can provide the JDBC connection object to Hibernate, without setting the JDBC source through the configuration file, as follows:
Class.Forname ("com.mysql.jdbc.driver);
String url = "jdbc: mysql: // localhost: 3306 / hibernatetetest? User = root & password ="
Java.sql.connection conn = drivermanager.getConnection (URL);
SessionFactory sessionFactory = cfg.buildsessionFactory ();
Session session = sessionFactory.openSession (conn);
Of course, you can also configure the JDBC source through the properties file hibernate.properties, for example:
Hibernate.properties
Hibernate.show_sql = TRUE
Hibernate.diaforct = 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
If you are through the XML file hibernate.cfg.xml, it is configured as follows:
Hibernate.cfg.xml
XML Version = '1.0' encoding = 'big5'?>>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
session-factory>
Hibernate-Configuration>
Hibernate is optional on the database connection pool, you can use the C3P0 connection pool when you have hibernate.c3p0. *, You will automatically enable the C3P0 connection pool, and your classpath must be Including C3P0-0.8.4.5.jar, the properties file hibernate.properties configuration example is as follows:
Hibernate.properties
Hibernate.show_sql = TRUE
Hibernate.diaforct = 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
If you are using hibernate.cfg.xml to configure the C3P0 connection pool as follows:
Hibernate.cfg.xml
XML Version = '1.0' encoding = 'big5'?>>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
session-factory>
Hibernate-Configuration>
You can also use the Proxool or DBCP connection pool, just configure hibernate.proxool. Or hibernate.dbcp. Or other in the configuration file, you can find the configuration example in hibernate.properties in the Hibernate's ETC directory, of course Remember to join the relevant JAR file in ClassPath. If you use Tomcat, you can also get the connection through the DBCP connection pool it provides, you can first refer to this article to set Tomcat's DBCP connection pool: DBCP connection pool setting
After setting the DBCP connection pool provided by the container, you just join the Connection.DataSource property in the configuration file, for example, in Hibernate.cfg.xml:
Hibernate.cfg.xml
If you are in hibernate.properties, join:
Hibernate.properties
Hibernate.Connection.DataSource = Java: Comp / Env / JDBC / DBNAME