How to configure JNDI operation

xiaoxiao2021-03-06  35

JNDI configuration reality under Tomcat5.0

------- Development Environment: Tomcat5.0 MS SQL Server2000 JDK1.4

One. What is JNDI:

Java Naming and Directory Interface (Name and Directory Service). If you have built a database connection pool, you will deeply understand the value of an excellent specifier to handle this problem. JNDI is a very important standard in J2EE, usually we are used in EJB programming. A pool operation can be implemented through JNDI.

2. Pre-configuration environment requirements: 1. Install JDK1.4, Tomcat5.0, MS SQL Server2000

2. Installing Microsoft SQL Server 2000 Driver for JDBC (the configuration process of this item is: put the msutil.jar under its lib into Tomcat / Common / LIB, then set the corresponding classpath can)

3. There is jtds.jar and put it in the $ TOMCAT5.0_HOME / COMMON / LIB directory (only here). Use it because Microsoft's Java SQL Server driver does not support secondary query, JTDS-0.6.jar is currently used

4. The JAR file required: commons-pool.jar, common-dbcp.jar (how to use it later)

Third.jndi configuration process:

1. Start Tomcat, open IE (enter http: // localhost: 8080 / admin in the address bar) to enter the management interface of Tomcat;

2. Enter the Data Sources item, select Create New Data Source in the drop-down menu in the upper right corner, enter the required configuration information in the input box below it:

JNDI NAME: JDBC / XXX (XXX named, JDBC / XXX is established JNDi entity name)

Data Source URL: JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = ObjectDatabaseNameName (Target Data Source Setting)

JDBC Driver Class: com.microsoft.jdbc.sqlserver.sqlserverdriver is a database driver

User name: User name to connect to the database

Password: Database password

Max. Active Connections: Maximum connection

Max. Idle Connections: is the maximum number of idle connections

Max. Wait for Connection: Maximum Waiting Connection

3. If you want to deploy a web application, place the application in the $ TOMCAT_HOME / WebApps directory, there are two ways to reference the JNDI data source we have configured.

(1) [This method can make multiple projects, the construction and release] You can create a new XXX.xml file under Tomcat_Home / Conf / Catalina / LocalHost, XXX is the name of the released web application, such as the web I posted. The application is JNDITEST, then the .xml file is named JNDITEST.XML, which is:

factory

org.apache.commons.dbcp.basicDataSourceFactory

Password

DatabasePassword

maxactive

4

maxwait

5000

URL

JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = ObjectDatabaseName

driverclassname

com.microsoft.jdbc.sqlserver.sqlserverdriver

Username

DatabaseUsername

maxidle

2

(2) The above method can only implement one project, it is recommended to be skilled in the future use (1) to create the XXX.xml file in order to avoid each web application, you can modify the server.xml file under Tomcat_Home / Conf, modify as follows:

Join between :

URL

JDBC: Microsoft: SQLServer: // localhost: 1433; DatabaseName = ObjectDatabaseName

driverclassname

com.microsoft.jdbc.sqlser.sqlserverdriver

maxwait

5000

maxactive

4

Password

DatabasePassword

maxidle

2

Username

DatabaseUsername

factory

org.apache.commons.dbcp.basicDataSourceFactory

(3) Join SQLServer Datasource Example JDBC / SQLSERVERDB javax.sql.datasource container

IV. Test: After the above work is completed, it can be tested. First, use ordinary JSP page testing without database operation, and after the database operation test is performed. (1) Creating a data source TEST, including table T_test, and as data (2) deploying commons-pool.jar under the lib file of the project, as the JSP test page code as follows

<% @ Page ContentType = "text / html; charset = GB2312"%>

<% @ Page Import = "java.sql. *"%>

<% @ Page Import = "javax.sql. *"%>

<% @ Page Import = "javax.naming. *"%>

<% Context ctx = new initialContext ();

Connection conn = NULL;

CTX = New InitialContext ();

DataSource DS = (Datasource) CTX.lookup ("Java: Comp / Env / JDBC / TEST");

CONN = ds.getConnection ();

Statement Stmt = Conn.createStatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata);

String SQL = "SELECT * from T_test";

ResultSet RS = Stmt.executeQuery (SQL);%> <% while (rs.next ()) {

Out.println (Rs.getstring (1));

Out.println (rs.getstring (2));

Out.println (Rs.getstring (3));

}%>

<% out.print ("Database Success!");

Rs.close ();

Stmt.close ();

CONN.CLOSE ();

%>

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

New Post(0)