Tomcat uses JNDI to connect the MySQL database

zhaozj2021-02-16  146

surroundings:

JDK1.4 Tomcat 5

I. Environment variable:

PATH: C: /J2SDK1.4.2/bin;

Java_Home: C: /J2SDK1.4.2/

Note: Only two.

two. JDBC driver

COPY The corresponding JDBC driver to Tomcat5 / Common / Libsql Server 2000 (or MySQL) for JDBC / LIB / Directory three.jar: msbase.jarmsqlser.jarmsutil.jar

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 chengji (Test Chengji in the Test Database)

Be

Tomcat5 / Conf / Catalina / Localhost

New file under the directory:

Myjsp.xml content is as follows:

URL jdbc: mysql: // localhost: 3306 / test

maxactive 4

maxwait 5000

driverclassname org.gjt.mm.mysql.driver -> com.mysql.jdbc.driver

username root

Password maxidle 2

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 exception ("Can't get context!"); context CTX = Context) INITCTX.LOOKUP ("Java: Comp / ENV"); // Gets 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 Chengji"; 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

转载请注明原文地址:https://www.9cbs.com/read-9079.html

New Post(0)