Tomcat5.5 Hibernate3 配置数据库连接池

一、在tomcat/common/lib目录下,添加数据库驱动。

二、修改server.xml,在<GlobalNamingResources>里面添加如下内容
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
username="xxx"
password="xxx"
driverClassName="org.gjt.mm.mysql.Driver"
maxIdle="10"
maxWait="5000"
url="jdbc:mysql://172.0.0.1:3306/mimms"
maxActive="50"/>
修改username,password,及url。上面是Mysql的驱动,如果使用其他数据库需要修改driverClassName。

三、修改content.xml,在<Context>里,添加
<ResourceLink name="jdbc/DBPool" type="javax.sql.DataSource" global="jdbc/DBPool"/>。

四、在工程的xml里,在<web-app>下,添加
<resource-ref>
<description>MySQL DB Connection Pool</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

五、修改hibernate.cfg.cml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.datasource">java:comp/env/jdbc/DBPool</property>
<property name="show_sql">false</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<mapping resource="com/xxx.hbm.xml"/>
</session-factory>
</hibernate-configuration>

六、测试是否连接成功,以下是一个servlet的测试代码:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//for test db, http://localhost:8080/xxx/Retrieval
public class Jdtest extends HttpServlet {

/**
* Constructor of the object.
*/
public Jdtest() {
super();
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

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 Which Accesses JNDI</TITLE></HEAD>");
out.println(" <BODY>");

try {
InitialContext initCtx = new InitialContext();
DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/DBPool");
Connection conn = ds.getConnection();

out.println("<br><br>");
out.println("Connection from DataSource successfully opened!<br>");

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from client");

//while (rs.next() ) {
rs.next();
String onerow = "Client info: " + " mobile: " + rs.getString("mobile") + "<br>";
out.println(onerow);
//}

rs.close();
stmt.close();
conn.close();
initCtx.close();

out.println("<br><br>");
out.println("Connection from DataSource successfully closed!<br>");
}
catch(Exception e) {
out.println("<br><br>");
out.println("Connection from DataSource NOT successfully opened!<br>");
out.println(e.getMessage() + "<br>");
}

out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

public void init() throws ServletException {
// Put your code here
}

} //End class Retrieval

七、在工程的web.xml,添加
<servlet>
<servlet-name>Retrieval</servlet-name>
<servlet-class>com.fractalist.mimms.register.persistence.Jdtest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Retrieval</servlet-name>
<url-pattern>/Retrieval</url-pattern>
</servlet-mapping>

注:使用的hibernate代码不需要修改。

你可能感兴趣的:(sql,mysql,Hibernate,servlet,jdbc)