Tomcat believes that everyone is already familiar. As a free and powerful Java Web Server, I got a lot of Java enthusiasts, the latest version of Tomcat5 support servlet2.4 and jsp2.0, today I will use Tomcat5 and MS SQLServer 000 Start the database connection pool configuration together.
Preparation required
1, JDK I use version 1.4.01
2, Tomcat 5 I am using the 5.0.16 version download address: http://jakarta.apache.org/site/binIndex.cgi
3, MSSQL Server 2000 database
4, the official JDBC Driver of MSSQL Server 2000, you can download Microsoft's official website for free download
After installation of the software installed, I will enter the configuration.
First, find the JDBC installation directory, put the msbase.jar and mssqlser.jar below the lib directory, Msutil.jar three files Copy to $ catalina_home / common / lib / ($ CATALINA_HOME representative is your Tomcat5 installation directory)
Second, use the text editor, I am using Editplus to open the $ catalina_home / conf / server.xml file using EditPlus, find the configuration configured Context, put the following code
Paste into the file
Debug = "5" reloadable = "true" crossContext = "true"> prefix = "localhost_dbtest_log." suffix = ". txt" TimeStamp = "true" /> Auth = "container" TYPE = "javax.sql.datasource" /> parameter>
Configure your mysqld max_connections large enough to handle All of Your DB Connections. Set to 0 for no limit. -> parameter>
Set to 0 for no limit. -> parameter>
IN ms, in this example 10 seconds. An Exception Is Thrown IF THIS TIMEOUT IS EXCEEDED. SET to -1 to wait indefinitely. -> parameter> parameter> parameter> parameter> parameter> Resourceparams> Context> Note: The password of the SA of my local database is empty, the database is Northwind, my directory name dbtest, his directory is d: / Rautinee Work / DB / Open the web.xml file below dbtest, replace the original content with the following code Xml Version = "1.0" encoding = "ISO-8859-1"?>
"- // Sun Microsystems, Inc.//dtd Web Application 2.3 // En" "http://java.sun.com/dtd/web-app_2_3.dtd"> resource-ref> web-app> OK, the configuration is complete, the following work is to write two files to test, whether the connection is successful. Here I use http://jakarta.apache.org above example The first is the bean file Package foo; Import javax.naming. *; Import javax.sql. *; Import java.sql. *; Public class dbtest { String foo = "not connection"; INT bar = -1; Public void init () { Try { Context ctx = new initialContext (); IF (ctx == null) Throw New Exception ("Boom - No Context"); DataSource DS = (Datasource) CTX.lookup ("Java: Comp / Env / JDBC / TESTDB"); IF (ds! = null) { Connection conn = ds.getConnection (); IF (conn! = null) { Foo = "Got Connection" Conn.toT7tring (); Statement Stmt = conn.createstatement (); ResultSet RST = Stmt.executeQuery ("SELECT * from Orders"); IF (Rst.Next ()) { Foo = Rst.getstring ("Customerid"); Bar = Rst.Getint ("ORDERID"); } CONN.CLOSE (); } } } catch (exception e) { E.PrintStackTrace (); } } Public string getfoo () {return foo;} Public int getbar () {return bar;} } Then INDEX.JSP file hEAD>
<%
Foo.dbtest TST = New foo.dbtest ();
TST.INIT ();
%>
FOO <% = TST.GETFOO ()%>
Bar <% = TST.GetBar ()%>
body>
html>
Compile operation, if you don't accident, you should retrieve a record.
What is shown in my IE is
MS SQL Server 2000 Java Search Results
Foo Vinet
Bar 10248
OK, the formulation is successful!
Reference documentation:
Http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-Doc/jndi-datasource-examples-howto.html There is mysql and oracle8i connection tutorial, interested friends can go to see. // ************** If you want to reprint the above article, please keep this information, thank you! **********
Author 海仔 Email: Rautinee@21cn.com http://www.tryitsoft.com
// ************** If you want to reprint the above article, please keep this information, thank you! **********