JNDI is a very important standard in J2EE, usually in the EJB programming, Tomcat4.0 provides methods of using JNDI directly in JSP and Servelt, talk about configuring and using JNDI in Tomcat 4.0. Method (with the JNDI connection database as an example) assume that the database is mysql, the experiment example is in the Tomcat_Home / WebApps / dbtest directory
A. Add mysql JDBC connection library mm.mysql-2.0.9-bin.jar into Tomcat_Home / Common / LIB
B. Configure the Tomcat_Home / conf / sere.xml file to join a context:
C. Add
<-!.. Maximum number of dB connections in pool Make sure you configure your mysqld max_connections large enough to handle all of your db connections Set to 0 for no limit -.>
D. Using JSPH or Servlet The following is the code in JSP files about JSP file (file name UserHandLeDb.jsp) Need to pay attention to JNDI Name To add "Java: Comp / ENV /"
<% @ page language = "java"%> <% @ page import = "java.util. *"%> <% @ page import = "java.sql. *"%> <% @ page import = "javax. SQL. * "%> <% @ Page Import =" javax.naming. * "%>
<% String jndi_name = "java: comp / env / jdbc / testdb"; string select_user_sql = "SELECT Userid, Name, Birthday, Email from EMP"; String ColNames [] [] = {{"User ID", "Name" , "Birth day", "email"}, {"userid", "name", "birthday", "email"}}; vector useerSet = new vector (); Vector columnset = new vector ();
For (int i = 0; i Context ctx = new initialcontext (); if (ctx == null) throw new exception ("no content"); DataSource DS = (Datasource) ctx.lookup (JNDI_NAME); Connection conn = ds.getConnection (); try {PreparedStatement psPreparedStatement = conn.prepareStatement (select_user_sql); ResultSet resultSet = psPreparedStatement.executeQuery (); while (resultSet.next ()) {columnSet = new Vector (); for (int i = 0; i E. Quote UserHandLeDb.jsp