jsp测试界面
<%@ page language="java" import="java.util.*,javax.naming.*,javax.sql.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Context initContext = new InitialContext();
/*Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/mytest001");*/
DataSource ds=(DataSource)initContext.lookup("java:/comp/env/jdbc/mytest001");
Connection conn = ds.getConnection();
//out.write(conn.toString());
//测试通过:jdbc:mysql://localhost:3306/mytest001, UserName=root@localhost, MySQL-AB JDBC Driver
//测试通过:com.mchange.v2.c3p0.impl.NewProxyConnection@15dab55
//用一个查询实例:我的数据库名是:mytest001 其中的表是:zhu_ce
String url="select * from zhu_ce";
PreparedStatement ps=conn.prepareStatement(url);
ResultSet rs=ps.executeQuery();
while(rs.next()){
%>
ID:<%=rs.getInt("id")%>;用户名:<%=rs.getString("username")%>;密码:<%=rs.getString("password")%>
<br/>
<%
}
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
//测试通过在浏览器上显示了数据库的所有信息
%>
</body>
</html>
在WEB-INF中的web.xml中配置以下属性(也可以不用,感兴趣的可以试试哈)
<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/mytest001</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
DBCP的context.xml配置
因为tomcat中在lib中自带DBCPjar包,所以使用DBCP数据源连接数据库的小伙伴们就不需要拷贝DBCPjar包了,但是记得把mysql的jar拷贝到lib中,接下来只要配置context.xml就可以了,(tips:想进一步研究的可以进入tomcat网址中查看里面的有关DBCP的详细配置),当然你可以写在tomcat中conf/context.xml中,也可以在你工程的=META-INF中写一个context.xml配置文件(我把注释加上了,不需要自行删除):
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- maxActive: Maximum number of database connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->
<!-- maxIdle: Maximum number of idle database connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWait: Maximum time to wait for a database 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.
-->
<!-- username and password: MySQL username and password for database connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->
<!-- url: The JDBC connection url for connecting to your MySQL database.
-->
<Resource name="jdbc/mytest001"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mytest001"/>
</Context>
c3p0的context.xml配置
对于c3p0首先记得拷贝其jar包(貌似有三个,包含Oracle的jar包)到tomcat中的lib中,然后就是对应的数据库jar包了,我演示的是mysql当然就把mysql的jar包拷贝到lib中了,一下是context.xml的配置:
<Resource auth="Container"
description="DB Connection"
driverClass="com.mysql.jdbc.Driver"
maxPoolSize="50"
minPoolSize="2"
acquireIncrement="1"
name="jdbc/mytest001"
user="root"
password="root"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
jdbcUrl="jdbc:mysql://localhost:3306/mytest001?autoReconnect=true" />
老习惯吧!最后说一下:
接着上一遍介绍java工程连接数据库配置和测试,今天带来的是web工程的数据源连接数据库的配置和测试。分享为了共同进步,学习永无止境,以飨喜欢的小伙伴们,共同加油吧^–^
当然欢迎大神的纠错和建议,谢!