Body:
Go to the Management page and create datasource: JNDI name: JDBC/MySQL
Data source URL: JDBC: mysql: // localhost: 3306/CS
JDBC Driver Class: COM. MySQL. JDBC. Driver
User name: Root
Password :********
Max. Active connections: 4
Max. idle connections: 2
Max. Wait for connection: 5000
Validation query:
% Tomcat_home % \ conf \Web. XML,In</Web-app>Before:
<Resource-ref>
<Description> dB connection </description>
<Res-ref-Name> JDBC/MySQL </RES-ref-Name>
<Res-type> javax. SQL. datasource </RES-type>
<Res-auth> container </RES-auth>
</Resource-ref>
Add
<Resourcelink name = "JDBC/MySQL" Global = "JDBC/MySQL" type = "javax. SQL. Cer CER"/>
TestCode
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"
Http://www.w3.org/TR/REC-html40/strict.dtd>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "javax. SQL. *" %>
<% @ Page import = "javax. Naming. *" %>
<% @ Page session = "false" %>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> </title>
<%
Out. Print ("My test started");
Datasource DS = NULL;
Try {
Initialcontext CTX = new initialcontext ();
DS = (datasource) CTX. Lookup ("Java: COMP/ENV/jdbc/MySQL ");
Connection conn = Ds. getconnection ();
Statement stmt = conn. createstatement ();
//Tip:UsersMust be an existing table in the database,
//The database mentioned aboveData source URLThe database contained in the configuration.
String strsql = "select * from users ";
Resultset rs = stmt.exe cutequery (strsql );
While (Rs. Next ()){
Out. Print (Rs. getstring (1 ));
}
Out. Print ("My test is complete");
}
Catch (exception ex ){
Out. Print ("Exception: the message is: "+ Ex. getmessage ());
Ex. printstacktrace ();
}
%>
</Head>
<Body>
</Body>
</Html>
Category:JavaRelease date: