Design and modify the context. XML and Web. xml files for the Web application configuration data source. Add the <resource> element defining the data source to the context. xml file and the <resource-ref> element to the Web. xml file. The element declares that the Web application references a specific data source. In addition, copy the mysqldriver. jar file of the MySQL JDBC drive class library to the <catalina_home>/lib directory.
- Add the <resource> element to contex. xml:
<Resource> elements are used to define JNDI resources. In tomcat, the data source is a type of JNDI resource. Create a context. xml file in the project/META-INF/that defines a data source named JDBC/databasename for the project application.
Context. xml file used to define the data source
1 <? XML version = " 1.0 " Encoding = " UTF-8 " ?> 2 <Context reloadable = " True " > 3 <Resource Name = " JDBC/databasename " Auth = " Container " Type = " Javax. SQL. datasource " 4 Maxactive = " 100 " Maxidle = " 30 " Maxwait = " 1000 " 5 Username = " Username " Password = " Userpassword " 6 Driverclassname = " Com. MySQL. JDBC. Driver " 7 Url = " JDBC: mysql: // localhost: 3306/databasename? Autoreconnect = true " /> 8 </Context>
2. Add the <resource-ref> element to Web. xml.
The element referenced by the resource is <resource-ref>. The following statements reference the JDBC/databasename data source.Code:
Content added to Web. xml
1 < Web-app > 2 < Resource-ref > 3 < Description > DB connection </ Description > 4 < Res-ref-name > JDBC/databasename </ Res-ref-name > 5 < Res-type > Javax. SQL. datasource </ Res-type > 6 < Res-auth > Container </ Res-auth > 7 </ Resource-ref > 8 </ Web-app >
3. JSP Example for connecting to the database through the data sourceProgram
The difference between datasource and jdbc api access is that the method for obtaining database connections is different (rows 19-22)
1 <% @ Page Language = " Java " Contenttype = " Text/html; charset = UTF-8 " 2 Pageencoding = " UTF-8 " %> 3 <% @ Page import = " Java. Io .* " %> 4 <% @ Page import = " Java. util .* " %> 5 <% @ Page import = " Java. SQL .* " %> 6 <! Doctype HTML public " -// W3C // dtd html 4.01 transitional // en " " Http://www.w3.org/TR/html4/loose.dtd " > 7 <HTML>8 <Head> 9 <Meta http-equiv = " Content-Type " Content = " Text/html; charset = UTF-8 " > 10 <Title> testjdbcapi </title> 11 </Head> 12 <Body> 13 <% 14 Try { 15 Connection con; 16 Statement stmt; 17 Resultset RS; 18 19 // Database connection from the data source 20 Context CTX = New Initialcontext (); 21 Datasource DS = (datasource) CTX. Lookup ( " Java: COMP/ENV/jdbc/databsename " ); 22 Con = DS. getconnection (); 23 24 // Create SQL statement 25 Stmt = Con. createstatement (); 26 // Add data 27 Stmt.exe cuteupdate ( " Insert specific insert statements " ); 28 29 // Select data 30 Rs = stmt.exe cutequery ( " Select statement " ); 31 32 // Out select result 33 Out . Println ( " <Table border = 1 width = 400> " ); 34 While (Rs. Next ()){ 35 String col1 = Rs. getstring ( 1 ); 36 String col2 = Rs. getstring ( 2 ); 37 String col3 = Rs. getstring ( 3 ); 38 Float Col4 = Rs. getfloat ( 4 ); 39 40 // Print datas 41 Out . Println (" <Tr> <TD> " + Col1 + " </TD> " 42 + " <TD> " + Col2 + " </TD> " 43 + " <TD> " + Col3 + " </TD> " 44 + " <TD> " + Col4 + " </TD> </tr> " ); 45 } 46 Out . Println ( " </Table> " ); 47 48 // Delete datas 49 Stmt.exe cuteupdate ( " Delete statement " ); 50 51 // Close 52 Rs. Close (); 53 Stmt. Close (); 54 Con. Close (); 55 56 } Catch (Exception e ){ 57 Out . Println (E. getmessage ()); 58 } 59 %> 60 </Body> 61 </Html>