连接池的配置步骤

1.首先在你的tomcat/conf/servlet.xml中添加

<Context path="**" docBase="**" debug="0" reloadable="true">
                        
     <Resource
     name="jdbc/test" auth="Container"
     type="javax.sql.DataSource"
     driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
     url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=**"
     username="sa"
     password="sa"
     maxAction="20" maxIdle="10" maxWait="-1"/>
     
     
     
    </Context>




2.建设连接

public synchronized Connection getConnection() {
try {
Context initContext = new InitialContext();
if (initContext == null) {
log.error("No Context");
throw new SQLException("No Context!");
}
DataSource db = (DataSource) initContext.lookup("java:comp/env/jdbc/test");
if (db == null) {
log.error("No dataSource");
throw new SQLException("No dataSource!");
}
conn = db.getConnection();
} catch (Exception e) {
e.printStackTrace();
log.error("Acquire DataBase Connection Exception: " + e.getMessage());
}
System.out.println("开始");
System.out.println(conn);
System.out.println("结束");
return conn;

}

你可能感兴趣的:(java,tomcat,jdbc)