Tomcat6.0数据库连接池的配置

来源:互联网 发布:sqlserver时间戳转换 编辑:程序博客网 时间:2024/04/27 23:14

Tomcat Context.xml下配置:
  <Resource name="jdbc/mysql" 
       auth="Container"        
       type="javax.sql.DataSource" 
       driverClassName="com.mysql.jdbc.Driver" 
       url="jdbc:mysql://localhost/ycx" 
       username="root" 
       password="flf" 
       maxActive="100" 
       maxIdle="30" 
       maxWait="10000" /> 

app web.xml下配置:

  <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>

test.jsp
<!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 begin !!    ");  
   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 admin";  
   ResultSet rs = stmt.executeQuery(strSql);  
   while(rs.next()){  
      rs.getString("admin_name");
      out.print(rs.getString(1));                   
     out.print(rs.getString("admin_name")+"/n");                   
     }  
out.print("   test is over");  
   }  
   catch(Exception ex){  
       out.print("ERROR:"+ex.getMessage());  
    ex.printStackTrace();  
   }  
%>  
</head>  
<body>  
</body>  
</html>  

原创粉丝点击