LZ has recently studied java web. Today, I just saw hibernate and found that the information on the Internet was too long when I configured the data source in hibernate. Generally, most of the configurations in tomcat version 5 are used. The configuration in tomcat 7 is slightly changed, and it is difficult for new users to find information, which may be slightly affected. Therefore, you can share the information with us and make a memo! If you have any mistakes, please do not hesitate to give me your advice!
Official integration documentation: These libraries are located in a single JAR at $ CATALINA_HOME/lib/tomcat-dbcp.jar.
These library files are all located in a separate JAR file, and the path to this file is/lib/tomcat-dbcp.jar.
2.Add the following before modifying the Tomcat_Home/conf/server. xml tag
<Resource name = "jdbc/TestDB" auth = "Container" type = "javax. SQL. DataSource"
MaxActive = "100" maxIdle = "30" maxWait = "10000"
Username = "javauser" password = "javadude" driverClassName = "com. mysql. jdbc. Driver"
Url = "jdbc: mysql: // localhost: 3306/javatest"/>
3.Add reference to context. xml
<ResourceLinkName = "jdbc/TestDB" global = "jdbc/TestDB"
Type = "javax. SQL. DataSource"/>
4. In your projectAdd <resource-ref> to 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. There are many tutorials which will not be detailed here.
In addition, note that the data source can only be used in web projects.
Write a simple servlet to output the hibernate session object again:
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 is called when a form has 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 simple, I believe you can do 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 ("<HEAD> <TITLE> A Servlet </TITLE> </HEAD> ");
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
}
}
Returns a session object.