server|sqlserver1. 进入tomcat目录下的conf文件夹目录里,找到Server.xml在tomcat的Server.xml文件中在</Context>标签与</Host>标签里,定议连接语句,格式如下
<!—path定议你的应用程序的目录所 ,/DBTest表示在Tomcat Webapps目录下à
<!—docBase=”DBTest” 表示你执行止程序时路径名称,例如以下的执行路径就是http://localhost:8080/DBTest--> <Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true">
<!-- maxActive: 连接池的最大数量,要确保有足够的连接数-->
<!-- maxIdle: 最大空闲连接数,设置为-1即表示不限制-->
<!-- maxWait:最长等待连接时间(最大等待连接池反回可用的时间), 以纳秒为单位,即设为10000相等于10秒,如果设置成-1表示不确定-->
<!-- username and password: 连接数据库使用的帐号与密码 -->
<!-- driverClassName:连接数据库的驱动程序,如SQLServer就是
com.microsoft.jdbc.sqlserver.SQLServerDriver.-->
<!-- url: 连接数据库路径,如
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=KB_Rate-->
<Resource name=" Default_JDBC " auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="javauser" password="javadude" driverClassName=" com.microsoft.jdbc.sqlserver.SQLServerDriver " url=" jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=KB_Rate""/>
</Context>
配置完成后,在DBTest目录下添加一个Test文件,如下:<% page contentType="text/html;charset=gb2312"%><% page import="java.sql.*"%><% page import="javax.sql.*"%><% page import="javax.naming.*"%><% Connection conn = null; Context initCtx = new InitialContext(); if (initCtx == null) throw new Exception("不能获取Context!"); Context ctx = (Context) initCtx.lookup("java:comp/env"); Object obj = (Object) ctx.lookup("/Default_JDBC ");//获取连接池对象 javax.sql.DataSource ds = (javax.sql.DataSource) obj; //类型转换 conn = ds.getConnection(); Statement stmt = conn.createStatement(); PreparedStatement ps=conn.prepareStatement("select * from FinalOrderdata"); ResultSet rs=ps.executeQuery(); while(rs.next()){ out.println(rs.getString(1)+"
"); i++; }rs.close();stmt.close();conn.close(); out.println("连接池测试成功"+i);
到于TOMCAT5.0版本的设置也是差不多,不过在Server.xml中添加连接池设置语句上有所有不,可参考http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html其它操作也是差不多,<Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true"><Context path="/DBTest" docBase="DBTest" debug="5" reloadable="true" crossContext="true"> <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_DBTest_log." suffix=".txt" timestamp="true"/> <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/TestDB"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <!-- Maximum number of dB connections in pool. Make sure you configure your mysqld max_connections large enough to handle all of your db connections. Set to 0 for no limit. --> <parameter> <name>maxActive</name> <value>100</value> </parameter> <!-- Maximum number of idle dB connections to retain in pool. Set to -1 for no limit. See also the DBCP documentation on this and the minEvictableIdleTimeMillis configuration parameter. --> <parameter> <name>maxIdle</name> <value>30</value> </parameter> <!-- Maximum time to wait for a dB connection to become available in ms, in this example 10 seconds. An Exception is thrown if this timeout is exceeded. Set to -1 to wait indefinitely. --> <parameter> <name>maxWait</name> <value>10000</value> </parameter> <!-- MySQL dB username and password for dB connections --> <parameter> <name>username</name> <value>javauser</value> </parameter> <parameter> <name>password</name> <value>javadude</value> </parameter> <!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next if you want to use this driver - we recommend using Connector/J though <parameter> <name>driverClassName</name> <value>org.gjt.mm.mysql.Driver</value> </parameter> --> <!-- Class name for the official MySQL Connector/J driver --> <parameter> <name>driverClassName</name> <value>com.mysql.jdbc.Driver</value> </parameter> <!-- The JDBC connection url for connecting to your MySQL dB. The autoReconnect=true argument to the url makes sure that the mm.mysql JDBC Driver will automatically reconnect if mysqld closed the connection. mysqld by default closes idle connections after 8 hours. --> <parameter> <name>url</name> <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value> </parameter> </ResourceParams></Context>有不足的地方,请各为指正!
还有,的就是请大虾门可以告诉一下小弟,如何设置法才能让连接池做到最高效,因为我试过用TOMCAT连接池连池,发现比较慢,反应较久,我不知道设错了那里,,!!我的设置如下:<Context path="/KB_Rate" docBase="/KB_Rate" debug="5" reloadable="true" crossContext="true"> <Resource name="Default_JDBC" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="1234" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=KB_Rate"/></Context>