timestamp="true"/>
type="javax.sql.DataSource"/>
2.修改webapps/ROOT/INF-WEB/web.xml
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
3.测试用的testdb.jsp内容:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import= "java.sql.* " %>
<%@ page import= "javax.naming.* "%>
<%
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/MysqlDB");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " insert into test(id,name) values('007','hello,中国!') ";
stmt.executeUpdate(strSql);
strSql = " select id,name from test ";
ResultSet rs = stmt.executeQuery(strSql);
while(rs.next()){
out.println(rs.getString(1));
out.println(rs.getString(2)+"
");
}
conn.close();
}catch(Exception ex){
ex.printStackTrace();
throw new SQLException("cannot get Connection pool.");
}
%>