配置连接池(终于弄好了~~)

server.xml

 <Context path="/bookstore" docBase="bookstore" debug="0"     reloadable="true">

     <Resource
         name="jdbc/BookDB" 
         auth="Container"
         type="javax.sql.DataSource"
         driverClassName="com.mysql.jdbc.Driver" 
         maxActive="100"
         maxIdle="30" maxWait="10000"
         url="jdbc:mysql://localhost:3306/bookdb?autoReconnect=true"
         username="dbuser" 
         password="1234" 
         removeAbandoned="true"/>

    </Context>




web.xml

<?xml version="1.0" encoding="UTF-8"?>
   <web-app>
    <welcome-file-list>
        <welcome-file>Dbjsp1.jsp</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
 <resource-ref>
  <description>DB connection</description>
  <res-ref-name>jdbc/BookDB</res-ref-name>
  <res-type>javax.sql.Datasource</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
 </resource-ref>
</web-app>



//数据库连接代码

  Connection con;
  Statement stmt;
  ResultSet rs;
  
  Context ctx=new InitialContext();
  DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/BookDB");
  con=ds.getConnection();
  
  stmt=con.createStatement();


你可能感兴趣的:(配置连接池(终于弄好了~~))