It seems that the data source configuration of tomcat6 is relatively simple, but if you set it in the previous version, there will be many problems, such:
Name JDBC is not bound in this context
Or cannot create JDBC driver of class ''for connect URL 'null', etc.
I will write down the steps for configuring the data source with atat6 and mysql5.17. You can refer
1. Download The mysql-connector-java-5.1.7-bin.jar and put it in the Lib folder under the tomcat installation directory;
2. Create a context. xml file under the META-INF folder under your project (create one if not, same as the WEB-INF folder;
The content is as follows:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Context>
- <Resource Name = "JDBC/performance_name" auth = "Container"
- Type = "javax. SQL. datasource" username = "root" Password = "xxxxxxx"
- Driverclassname = "com. MySQL. JDBC. Driver" url = "JDBC: mysql: // localhost/database_name"
- Maxactive = "8" maxidle = "4"
- Testonborrow = "true"
- Validationquery = "select 1"
- Timebetweenevictionrunsmillis = "30000"
- />
- </Context>
3. Replace the above performance_name with your data source name, database_name with your database name, and password with your
4. Check whether your Java code is successful.
- Initialcontext initcontext = new initialcontext ();
- Datasource DS = (datasource) initcontext. Lookup ("Java: COMP/ENV/jdbc/performance_name ");
- Connection conn = Ds. getconnection ();
- Statement PS = conn. createstatement ();
- Resultset rs = ps.exe cutequery ("select * From table_name ");
- While (Rs. Next ())
- {
- System. Out. println (Rs. getstring (2 ));
- }
5. The output information should be displayed.