3.修改项目WEB-INF/web.xml 配置文件(若无,请新建),在“”之上添加如下代码:
DB Connection
jdbc/mysql
javax.sql.DataSource
Container
上步中若修改了数据源名此步中红色部分请保持与上步中的一致。
4.代码示例:index.jsp测试界面
pageEncoding="UTF-8"%>
Insert title hereContext initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
String sql="select *from recorder where descid like '%tongcheng%'";
System.out.println(sql);
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}
%>
通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。
运行输出结果
select *from recorder where descid like '%tongcheng%'
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1446828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1445828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1444828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1443828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1442828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1441828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1440828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1439828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/mm/18-50/,1438828308831,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1447828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1446828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1445828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1444828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1443828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1442828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1441828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1440828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1439828309279,100000000
bbs tongcheng http://jiaoyou.58.com/ay/gg/18-50/,1438828309279,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1447828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1446828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1445828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1444828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1443828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1442828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1441828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1440828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1439828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/mm/18-50/,1438828309648,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1447828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1446828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1445828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1444828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1443828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1442828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1441828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1440828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1439828309997,100000000
bbs tongcheng http://jiaoyou.58.com/anqing/gg/18-50/,1438828309997,100000000
bbs tongcheng http://jiaoyou.58.com/ankang/mm/18-50/,1447828310465,100000000
bbs tongcheng http://jiaoyou.58.com/ankang/mm/18-50/,1446828310465,100000000