Tomcat7.0 connects to mysql5.0 Tomcat7Tomcat
BitsCN.com
1, mysql-connector-java-5.0.4-bin.jar into C:/Program Files/Apache Software Foundation/Tomcat 7.0/lib
2. the project's web. xml is consistent with tomcat
webt3
index.jsp
Tomcat 7 DBCP
jdbc/mysql5
javax.sql.DataSource
Container
3. context. xml in tomcat/conj
WEB-INF/web.xml
4. test: index. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ page import = "java. SQL. *, javax. SQL. *, javax. naming. * "%>
Insert title here<% Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; String SQL = null; String username = null; String alias = null; String pwd = null; string strMsg = null; try {Context cxt = new InitialContext (); DataSource ds = (DataSource) cxt. lookup ("java:/comp/env/jdbc/mysql5"); conn = ds. getConnection (); SQL = "SELECT 1"; stmt = conn. prepareStatement (SQL); rs1_stmt.exe cuteQuery (SQL); if (rs. next () {strMsg = "connection OK "; out. println ("connection OK");} else {out. println ("rs. next () fail ");} rs. close ();} catch (Exception e) {out. println ("connection failed:" + e. getMessage (); // e. printStackTrace ();} try {if (stmt! = Null) stmt. close ();} catch (Exception e) {}try {if (conn! = Null) conn. close ();} catch (Exception e) {}%>OK,
The operation is successful.
BitsCN.com