Tomcat7.0 connects to mysql5.0 _ MySQL

Source: Internet
Author: User
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

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.