At first, I was learning from the Lonely Wolf's Jndi tutorial to do, he spoke very detailed, but the disadvantage is because he said too detailed. Check a lot of books, are suggested that do not use the global to configure Jndi, one is to modify Tomcat Server.xml, easy to destroy Tomcat, followed by the thankless. I would recommend using local variables as well.
1. Step one: Before learning to configure Jndi, you must first master the Tomcat configuration virtual directory (you can go to the tomcat in the home directory configuration and virtual directory configuration issues such as the article, you can go to my Tomcat configuration virtual directory-thinking Focus NetEase Blog to find similar links). After learning about Tomcat's configuration virtual directory, you'll know why you should create an. xml file under the Tomcat installation directory \conf\catalina\localhost.
2. Step two: Start with the local configuration Jndi (this tutorial also speaks only of local configuration).
The following will be attached to the code, color, you have to change according to their actual situation.
3. Step three: Finally through the Tomcat Startup.bat boot and the Computer browser is able to see the successful connection to the data source and driver, but to start Tomcat with Eclipse can not be, always prompt cannot create JDBC driver of Class ' for connect URL ' null ' problem, later found Tomcat+mysql problem, could not find JDBC driver? , said JDBC for MySQL in addition to Mysql-connector-java-3.1.8-bin.jar a aspectjrt.jar, put in web-inf\lib inside, the result is no problem, can run. A lot of information and books on the internet did not mention this, I do not know why the need to copy Aspectjrt.jar this thing, it may be the Tomcat version or the JDBC version problem. If it doesn't work, you can try this again: Use Druid to configure the Jndi data source under Tomcat
--------------------------------------------------------------I'm a cute split-line--------------------------------------------------- --
Proceed to step two, with the code:
1. First, create a new Test_jndi project, and then in the MySQL and SQL Server databases, a database named Test (test both databases). Then, create the test_jndi.jsp in the project, and then put the code in the JSP
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <%@ page import=" java.sql.*,javax.sql.*,javax.naming.* "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >2. Then, in the. xml file under WebContent in the project directory,
Add the following code:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app id= "webapp_id" version= "2.4" xmlns= "Http://java.sun.com/xml /ns/j2ee "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/ Java http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><display-name>Test_JNDI</display-name>< welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>test_jndi.jsp</ Welcome-file> <welcome-file>default.htm</welcome-file><welcome-file>default.jsp</ welcome-file></welcome-file-list> <!--mysql database jndi data Source Reference--<resource-ref> <description& Gt MySQL DB connection</description> <res-ref-name>jdbc/mysql</res-ref-name> <res-type>ja Vax.sql.datasource</res-type> <res-auth>Container</res-auth> </resource-ref> <!--sqls Erver database Jndi Data Source Reference--<resource-ref> <description>sqlserver DB connection</description> <res-ref-name>jdbc/sqlserver</res-ref-name> <RES-TYPE&G T;javax.sql.datasource</res-type> <res-auth>Container</res-auth> </resource-ref> & Lt;/web-app>
3. Finally, and most importantly, the most error-prone is to create a test_jndi.xml file under the Tomcat installation directory \conf\catalina\localhost (the file name is preferably the same as the project name Test_jndi, not the same situation I have not experimented with)
(1). You can first add the following code to the Test_jndi.xml file, and then enter the URL in the browser to verify that you can access the project home page.
Note 1: The above code is a label without a label, which is not the same as the following code. Specifically, <context ..../> and <context ........... Here is the data source information </context > The two code syntax is different, one has a tag body.
Note The 2:xml file does not appear in any one of the Chinese, otherwise booting Tomcat will error (tomcat error Invalid byte 2 of 2-byte UTF-8 sequence)
Note 3: There must be no space in front of the <?xml in this code, or tomcat error (not allowed to match "[xx][mm][ll]" Processing instruction target)
(2). Paste the full code:
<?xml version= "1.0" encoding= "UTF-8"?> <context path= "/test_jndi" docbase= "E:/eclipseee/workspace/test_ Jndi/webcontent "reloadable=" true "debug=" 0 "crosscontext=" true "> <resource name= " Jdbc/mysql " Auth= "Container" type= "Javax.sql.DataSource" maxactive= " " maxidle= "" "Maxwait=" 10000 " Username= "root" password= "0821" driverclassname= "Com.mysql.jdbc.Driver" url= " jdbc:mysql:// Localhost:3306/test?useunicode=true&characterencoding=utf-8 "/> <resource name=" jdbc/ SQL Server " auth=" Container " type=" Javax.sql.DataSource "maxactive=" " maxidle= " Maxwait= "10000" username= "sa" password= "0821" driverclassname= " Com.microsoft.sqlserver.jdbc.SQLServerDriver " url=" jdbc:sqlserver://localhost:1433;databasename=test "/ > </Context>
Attention:
The user name and password are set to the database of your own computer, test is the database.
Name= "Jdbc/sqlserver", name in all places have to be consistent.
Path= the name of "/test_jndi" to be set sing Woo the same as this. xml file.
Run Successful results:
--------------------------------------------------------------I'm a cute split-line--------------------------------------------------- --
This article references: 1. Configuration of the Jndi data source-aloof and pale wolf
What exactly is 2.JNDI?
3. Home directory Configuration and virtual directory configuration issues in Tomcat
Warm tip: Paste the copy code into eclipse if the line number is left, you can take advantage of the Eclipse shortcut key alt+shit+a to delete the line number in bulk.
Original is not easy, please specify reprint: http://blog.csdn.net/sinat_31719925
Jndi Data source Local configuration (resolve cannot create JDBC driver of class ' for connect URL ' null ')