servlet|sql| Data | database | database connection
1. From aspx? Familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en#filelist ">http://www.microsoft.com/ Downloads/details.aspx? familyid=4f8f2f01-1ed7-4c4d-8f7b-3d47969e66ae&displaylang=en#filelist Download "Microsoft SQL Server Driver for JDBC ", and install, get Msbase.jar,mssqlserver.jar and Msutil.jar three files, copy three files to Tomcat 4.1 Common\lib folder
2. Add the following code to the host domain in the server.xml of Tomcat 4.1
Factory
org.apache.commons.dbcp.BasicDataSourceFactory
maxactive
Maxidle
maxwait
10000
&NBSP
username
SA
password
your password
driverclassname
com.microsoft.jdbc.sqlserver.SQLServerDriver
URL
jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs
 &NBSP
3. Create a new folder under WebApps "MyWeb", "Myweb\web-inf", "myweb\web-inf\classes"
4. Under Web-inf, create a new file "Web.xml" and add the following
"Java.sun.com/dtd/web-app_2_3.dtd" >HTTP://JAVA.SUN.COM/DTD/WEB-APP_2_3.DTD ">
My Web
Invoker
/servlet/*
Jdbc/mydb
Javax.sql.DataSource
Container
5. Writing servlet program Jdbcpoolserv.java
Import java.sql.*;
Import Javax.naming.Context;
Import Javax.sql.DataSource;
Import Javax.naming.InitialContext;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
public class Jdbcpoolserv extends HttpServlet
{
public void doget (HttpServletRequest request,
HttpServletResponse response) throws Servletexception,ioexception {
DataSource Ds=null;
Connection Con=null;
String val= "null", title= "JDBC pooling Test";
try{
Context Initctx = new InitialContext ();
if (Initctx = null)
throw new Exception ("boom-no context");
ds = (DataSource) initctx.lookup (
"Java:comp/env/jdbc/mydb");
if (ds!= null) {
con = ds.getconnection ();
if (con!= null) {
Statement stmt=con.createstatement ();
ResultSet rs=stmt.executequery ("select * from authors");
Rs.next ();
Val=rs.getstring ("au_id");
Rs.close ();
Stmt.close ();
}
Con.close ();
}
}
catch (Exception ex) {
System.out.println (Ex.getmessage ());
}
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("
"+ val +"
");
Out.println ("");
Out.println ("");
}
}
6. Compile Jdbcpoolserv.java get Jdbcpoolserv.class (Note add Servlet.jar package), copy it to "Myweb\web-inf\classes"
7. Start SQL SERVER2000
8. Start Tomcat
9. Browse Http://127.0.0.1:8080/myweb/servlet/JDBCPoolServ
10. In IE, you can see "172-32-1176"
Note: If you do not run correctly check the folder name above, and the URL and Java class name are in the same case