配置Tomcat连接池步骤

    Tomcat 貌似有连接池管理的模块,只要配置了数据库相关信息,并告诉web应用程序,web应用程序启动后,就可以从Tomcat那里获取连接。

 

/*软件配置:windows xp; tomcat7; mysql5.5*/

 

1.打开apache-tomcat-7\conf\context.xml,加入数据库资源信息:

 

<Resource name="jdbc/mysource" auth="Container" type="javax.sql.DataSource" 
		password="sikaijian" username="root" driverClassName="org.gjt.mm.mysql.Driver" 
		url="jdbc:mysql://localhost:3306/mybase" 
		maxActive="100" maxIdle="30" maxWait="5000"/> 

2. tomcat 需要mysql的驱动程序,所以要再lib目录中加入驱动JAR包,如:mysql-connector-java-5.1.20-bin.jar;

 

3.要让web应用知道用这么一个数据源,这么一个连接池可以使用,需要在web.xml中加入如下代码:

 

<resource-ref>
		<description>MySQL DataSource</description>
		<res-ref-name>jdbc/mysource</res-ref-name>
		<res-type>javax.sql.DataSource</res-type>
		<res-auth>Container</res-auth>
	</resource-ref>

 

完成以上三步,一个数据源就算配置完成了。

 

下面做了个简单的测试,JSP片段代码如下:

 

<% 
	try
	{
		Context initCtx=new InitialContext();
		DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/mysource");
		Connection conn=ds.getConnection();
		out.println("data from database:<br>");
		Statement stmt=conn.createStatement();
		ResultSet rs =stmt.executeQuery("select * from staff");
		%><table border="true" bordercolor="black">
			<tr>
				<td width="50">姓名</td>
				
			</tr>
		<%
		while(rs.next())
		{
		%><tr><%
			%><td width="50"><%out.println(rs.getString("name"));%></td><%
			
			%></tr><%
		}
		%></table><%
		rs.close();
		stmt.close();
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
%>
 

/*

    mysql创建数据库的时候,可以设置字符编码。创建表的时候也可以设置字符编码。

 

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值