1. Open the $ catalina_home/CONF/server. xml file and add the following script between <Context Path = "/dbtest" docbase = "dbtest" DEBUG = "5" reloadable = "true" crosscontext = "true">
<Resource Name = "JDBC/testjndi" auth = "Container" type = "javax. SQL. datasource "maxactive =" 100 "maxidle =" 30 "maxwait =" 10000 "username =" sa "Password =" xiaolangs "driverclassname =" com. microsoft. JDBC. sqlserver. sqlserverdriver "url =" JDBC: Microsoft: sqlserver: // 127.0.0.1: 1433; databasename = pubs "/>
</Context>
2. Create the dbtest folder under $ catalina_home/webapps and create the testjndi. jsp file. The file content is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<HTML>
<Body>
<%
Context CTX = new initialcontext ();
Connection conn = NULL;
CTX = new initialcontext ();
Datasource DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/testjndi"); // query the JNDI Data Source Name
Conn = Ds. getconnection ();
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from authors ";
Resultset rs1_stmt.exe cutequery (SQL );
While (Rs. Next () {%>
This 1 column content is: <% = Rs. getstring (1) %> <br>
This 2 column content is: <% = Rs. getstring (2) %> <br>
<%}
Out. Print ("the database operation is successful. Congratulations! ");
Rs. Close ();
Stmt. Close ();
Conn. Close ();
%>
</Body>
</Html>
3. Open http: // localhost: 8080/dbtest/testjndi. jsp
OK. In theory, it has been successful. If it fails, please find the cause by yourself, hey. For more information, see the references page.