首先在mysql中建立如图数据库:

Tomcat连接Mysql测试样例_第1张图片

1、
修改contex.xml文件:


                type="javax.sql.DataSource"
                maxActive="50" maxIdle="10" maxWait="5000"
                username="root" password="bobo365"
                driverClassName="org.gjt.mm.mysql.Driver"
                url="jdbc:mysql://localhost/test1" />


2、
修改web.xml文件:



            DB Connection
            jdbc/mysql
            javax.sql.DataSource
            Container
          

 


3、

测试文档test.jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>  
<%@ page import="javax.sql.*"%>  
<%@ page import="javax.naming.*"%>


  
    mysql test page
        
        
           
        
        
          
  
  
        <% 
    out.print("My data test:
");  
    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();
            String strSql = " select * from pet";
            ResultSet rs = stmt.executeQuery(strSql);
                   while(rs.next()){

                                        out.print("name:" + rs.getString(1));

                                        out.print("\towner:" + rs.getString(2));

                                        out.print("\tbirth:" + rs.getString("birth"));
                                        out.println("
");
                }
                rs.close();
                stmt.close();
                conn.close();
     }catch(Exception ex){
             ex.printStackTrace();
     }
        %> 
  

 

4、显示样式:

Tomcat连接Mysql测试样例_第2张图片

 

 

//////////////////////////////////////////////////////////////

启开tomcat管理界面。

1、更改配置文件:context.xml

Tomcat连接Mysql测试样例_第3张图片

2、界面显示:

Tomcat连接Mysql测试样例_第4张图片