Recently learning Hinernate, need to configure the Jdni data source in Tomcat, so I looked at a lot of information on the Internet, roughly the same.
1. Put the JDBC connection driver under the Tomcat installation directory Lib;
2. Add modified Context.xml File
3. Add modify Web. xml file
4. Write the connection test code.
Here is a personal summary to do a detailed explanation:
Tools: Eclipse Compiler, Tomcat 7, sqlserver2005 database
1. Document Structure:
2. Open the Servers project that is automatically generated when you deploy Tomcat in Eclpise
2.1. Add the following code to the Context.xml:
<resource name= "Jdbc/test"
Auth= "Container"
Type= "Javax.sql.DataSource"
Maxactive= "100"
Maxidle= "30"
Maxwait= "10000"
Username= "sa" password= "901030"
Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
Url= "Jdbc:sqlserver://localhost:1433;databasename=db_test"/>
Key noun Explanation:
Resource Name: For your data source name, be free (note the same as the following Web. XML configuration Code).
Username, password, daiverclassname, url: This everybody understands, according to own database actual connection statement fills in.
The rest: I do not understand, fixed unchanged.
2.2 Add the following code to Web. XML:
<!--jdni Data Source Configuration--
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
< note: where <res-ref-name> values must correspond to the <resource name> in the previous context.xml
3.Build Path imports the JDBC driver.
4. Write a JSP test.
<%@ page contenttype= "Text/html;charset=utf-8"%>
<%@ page import= "java.sql.*,javax.sql.*, Javax.naming.* "%>
<%! Connection CT; %>
<%! ResultSet rs; %>
<%! PreparedStatement PS; %>
<%! String username; %>
<%
Try
{
//Initialize Find namespace
Context ctx = new InitialContext ();
//Ctx.lookup ("Java: Comp/env/jdbc/test ") The code in parentheses" java:comp/env/"is required
//" Jdbc/test "is your resource name
DataSource ds = ( DataSource) ctx.lookup ("Java:comp/env/jdbc/test");
//Remove connection
ct = ds.getconnection ();
The table below queries itself defines
ps=ct.preparestatement ("select *from user_info");
Rs=ps.executequery ();
while (Rs.next ()) {
username=rs.getstring (1);
}
Out.println (username);
%>
<%
out.println ("Done!!!");
} catch (Namingexception e) {
System.out.println (e.getmessage ());
} catch (SQLException e) {
E.printstacktrace ();
}finally{
Ct.close ();
Ps.close ();
Rs.close ();
}
%>
Questions about TOMCAT configuration Jdni!!