surroundings:
JDK1.4 Tomcat 5
I. Environment variable:
PATH: C: /J2SDK1.4.2/bin;
Java_Home: C: /J2SDK1.4.2/
Note: Only two.
two. SQL Server driver
Copy
Microsoft SQL Server 2000 Driver For JDBC / LIB / Directory three .jar:
Msbase.jar
MSSQLServer.jar
Msutil.jar
TO
Tomcat5 / Common / LIB
Third. Virtual directory and connection pool
Suppose you want to build your own virtual directory in D: / jsgtest
/ myjsp
Suppose you want to connect the pool JNDI named AAA
Assume that the connection database is PUBS
Be
Tomcat5 / Conf / Catalina / Localhost
New file under the directory:
Myjsp.xml
The content is as follows:
xml version = '1.0' encoding = 'utf-8'?>
Resourceparams> context>
test:
Built in the D: / jsgtest directory: test.jsp: <% @ page contenttype = "text / html; charset = GB2312"%> <% @ page import = "java.sql. *"%> <% @ Page Import = "javax.sql. *"%> <% @ Page Import = "javax.naming. *"%> <% connection conn = null; context itis = new initialContext (); if (initctx == null) throw new new new Exception ("Can't get context!"); Context CTX = (Context) INitctX.lookup ("Java: Comp / ENV"); // Get the connection pool object Object obj = (Object) CTX.lookup ("aaa"); // Type conversion javax.sql.datasource DS = (javax.sql.datasource) obj; conn = ds.getConnection ();
STATEMENT Stmt = Conn.createStatement (); string strsql = "select * from [titles]"; ResultSet RS = stmt.executeQuery (strsql); while (rs.next ()) {Out.println (rs.getstring (1) );
}
Rs.close (); stmt.close (); conn.close ();
Out.println ("Successful connection pool test");%>
Test path:
Http: // localhost: 8080 / myjsp / test.jsp