Let's start with a little digression, LZ learning Java Web recently.
Today, I just saw hibernate, hair now hibernate configuration data source when the online information is too long, generally with the Tomcat 5 version of the configuration of the majority. and Tomcat 7 under the configuration slightly change, novice to find data difficult, may be slightly hit, so the collation of information and share with you. can also be used as a reminder. If there is something wrong with it. Also please advise!
1. Join the database driver package Mysql-connector-java-5.1.13-bin.jar into the Lib package under the Tomcat folder.
Note: Almost all version numbers on the web say they need to join Commons-dbcp-1.4.jar,commons-pool-1.5.4.jar and Commons-collections.jar.
However, Tomcat 7 does not have to be added to these jar files. These libraries are already integrated as a result of the Lib package for Tomcat 7.
The official integration documents are:These libraries is located in a single JAR at $CATALINA _home/lib/tomcat-dbcp.jar.
This means that the library files are in a separate jar file with the path of /lib/tomcat-dbcp.jar.
2. add before changing the tomcat_home/conf/server.xml,
<resource name= "Jdbc/testdb" auth= "Container" type= " Javax.sql.DataSource "
Span style= "font-size:14px" > maxactive= "100" maxidle= "maxwait=" 10000 "
username= "Javauser" password= "Javadude" driverclassname= "Com.mysql.jdbc.Driver"
url= "jdbc:mysql://localhost:3306/ Javatest "/>
3. Add references in Context.xml
<resourcelink name="jdbc/TestDB" Global="jdbc/TestDB"
type="Javax.sql.DataSource"/>
4. Add <resource-ref> in your project project's Web. Xml.
<resource-ref>
<description>tomcat DataSource Test,one MySQL datasource</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
5. Configure the database connection in the Hibernate.cfg.xml file, which has a lot of tutorials and is no longer detailed here.
Also, note that data sources are only available in Web projects.
Once again, write a simple servlet to output Hibernate's Session object:
The code is as follows:
Package Com.amaker.servlet;
import java.io.IOException;
import Java.io.PrintWriter;
import javax.servlet.ServletException;
import Javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import Com.amaker.util.HibernateUtil;
@SuppressWarnings("serial")
Public class Testservletextends httpservlet {
/**
* Constructor of the object.
*/
Public Testservlet () {
Super ();
}
/**
* Destruction of the servlet. <br>
*/
Public void Destroy () {
Super . Destroy (); Just puts "destroy" string in log
Put Your code here
}
/**
* The Doget method of the servlet. <br>
*
* This method was called when a form had its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param Response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
Public void doget (httpservletrequest request, httpservletresponse response)
throws Servletexception, IOException {
Hibernateutil util = new hibernateutil ();
The Hibernateutil class is used to return a session
Object. Very easy, I believe you can handle it. Session session = Util.getsession ( );
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println (");
Out.println ("<BODY>");
OUT.PRINTLN (session);
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}
Public void doPost (httpservletrequest request, httpservletresponse response)
throws Servletexception, IOException {
Doget (request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws servletexception If an error occurs
*/
Public void init ()throws servletexception {
Put Your code here
}
}
Interview results. Returns a Session object.
Hibernate in Tomcat7. Configure MySQL data source under X