Jar files:
Mysql-connector-java-5.1.7-bin.jar (* http://dev.mysql.com/downloads/connector/j/5.1.html of main files)
Commons-dbutils-1.1.jar (queryrunner package http://commons.apache.org/downloads/download_dbutils.cgi required)
Instance:
1. Put the required package in the lib directory of Tomcat.
2. Modify context. XML (in the conf directory of Tomcat)
Add the following code between <context>... <Context/>
<Resource Name = "JDBC/mysqlds" // The error cannot be found here. I wrote Ava:/COMP/ENV/jdbc/mysqlds here, so an error occurred.
Auth = "Container"
Type = "javax. SQL. datasource"
Maxactive = "100"
Maxidle = "30"
Maxwait = "10000"
Username = "root"
Password = "123456"
Driverclassname = "com. MySQL. JDBC. Driver"
Url = "JDBC: mysql: // 127.0.0.1/angelsix"/>
3. jsp connection code
Create a JavaBean: dbconn. Java
/********************************/
Package com. angelsixth;
Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. SQL. datasource;
Import org. Apache. commons. dbutils. queryrunner;
Public class dbconn {
Public static queryrunner getqueryrunner (){
// The data source object can be understood as the manager of the Connection Pool, through which the database connection can be obtained
Datasource DS = NULL;
Try {
// Obtain the data source object by setting the name of the data source in the context. xml file.
Context context = new initialcontext ();
DS = (datasource) Context. Lookup ("Java:/COMP/ENV/jdbc/mysqlds ");
} Catch (exception e ){
System. Out. println ("An error occurred while obtaining the data source ");
}
// Core class in dbutils. The data source object is passed when an object is generated.
Queryrunner QR = new queryrunner (DS );
Return QR;
}
}
/********************************/
The connection to Oracle is basically similar to that to MySQL:
First, the configuration file of Tomcat context. XML is as follows:
Note that oracleds is not using LES
<Resource Name = "JDBC/oracleds"
Auth = "Container"
Type = "javax. SQL. datasource"
Maxactive = "100"
Maxidle = "30"
Maxwait = "10000"
Username = "Scott"
Password = "tiger"
Driverclassname = "oracle. JDBC. Driver. oracledriver"
Url = "JDBC: oracle: thin: @ 127.0.0.1: 1521: orcl"/>
Public static queryrunner getrueryrunner (){
Datasource DS = NULL;
Try {
Context context = new initialcontext ();
System. Out. println ("iiii" + context );
//Obtain the data source object by setting the name of the data source object in the context. xml file.
DS = (datasource) Context. Lookup ("Java:/COMP/ENV/jdbc/Oracleds ");
} Catch (exception e ){
System. Out. println ("An error occurred while obtaining the data source ");
}
Queryrunner QR = new queryrunner (DS );
Return QR;
}
The call is:
Queryrunner = dbhelper. getrueryrunner ();
String SQL = "select * from CMS ";
List list = new arraylist ();
Try {
List = (list) queryrunner. Query (SQL, new beanlisthandler (cms. Class ));
} Catch (sqlexception e ){
E. printstacktrace ();
}
Request. setattribute ("list", list );
Request. getrequestdispatcher ("/test20110906/success. jsp ")
. Forward (request, response );