Open the context. xml file in the conf folder of Tomcat 6.0 and write it under the <context reloadable = "true"> label.
<Resource Name = "JDBC/mysqlds" auth = "Container" type = "javax. SQL. datasource "maxactive =" 100 "maxidle =" 30 "maxwait =" 10000 "username =" root "Password =" admin "driverclassname =" com. mySQL. JDBC. driver "url =" JDBC: mysql: // 127.0.0.1/blog "/>
And put the driver jar package in the Lib folder under Tomcat 6.0, and then use this section in the Java classCodeObtain:
Datasource DS = NULL; try {// use the context. xml file to set the name of the data source object and obtain the data source object. Context context = new initialcontext (); DS = (datasource) context. lookup ("Java:/COMP/ENV/jdbc/mysqlds");} catch (exception e) {system. out. println ("Database Retrieval error");} connection conn = Ds. getconnection ();
Then, use the queryrunner class:
Sqlqueryrunner QR = new queryrunner (DS); int result = QR. update (Conn, SQL, string []); // QR. update (SQL, string []); // call its update method to run the SQL statement and return the number of updates. insert into/update/delete list <blog> List = QR. query (SQL, new beanlisthandler (blog. class); // query. Blog. Class is JavaBean: blog. java. Stored in list.
@@@