An error occurred while connecting to the MySQL Oracle instance through JSP-database source Retrieval

Source: Internet
Author: User

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.