Hibernate in Tomcat7. Configure MySQL data source under X

Source: Internet
Author: User
Tags auth java web

Let's start with a little digression, LZ recently learning Java Web. I just saw hibernate today and found that the data on the web was too remote when hibernate was configured, usually in the version of Tomcat 5. 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, but also for the use of memo! If there is any improper, please do not hesitate to enlighten!



1. Add the database driver package Mysql-connector-java-5.1.13-bin.jar into the Lib package in the Tomcat directory.


Note: Almost all versions of the Web say you need to add Commons-dbcp-1.4.jar,commons-pool-1.5.4.jar and Commons-collections.jar.

However, Tomcat 7 does not have to add these jar files because these libraries are already integrated under the Tomcat 7 Lib package.

The official integration documents are:These libraries is located in a single JAR at $CATALINA _home/lib/tomcat-dbcp.jar.

The library files are located in a separate jar file, the path of which is /lib/tomcat-dbcp.jar.

2. before modifying 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> to your 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 many tutorials, which are no longer detailed here.

Also, note that the data source is available only 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 simple, I believe you can take care of. 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

}


}

Accesses the result and returns a Session object.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.