我的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/SqlServerDB");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " insert into test(id,name) values('00001','fany')";
stmt.executeUpdate(strSql);
strSql = " select id,name from test ";
ResultSet rs = stmt.executeQuery(strSql);
if(rs.next()){
out.println(rs.getString(1));
out.println(rs.getString(2));
}
}catch(Exception ex){
ex.printStackTrace();
throw new SQLException("cannot get Connection pool.");
}
%>
<%@ 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/SqlServerDB");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource)obj;
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql = " insert into test(id,name) values('00001','fany')";
stmt.executeUpdate(strSql);
strSql = " select id,name from test ";
ResultSet rs = stmt.executeQuery(strSql);
if(rs.next()){
out.println(rs.getString(1));
out.println(rs.getString(2));
}
}catch(Exception ex){
ex.printStackTrace();
throw new SQLException("cannot get Connection pool.");
}
%>
tomcat4.0的话,就是另外一种配法了。
见http://www.zdnet.com.cn/developer/code/story/0,2000081534,39134180,00.htm
我加了:<web-app>
<resource-ref>
<description>SqlServer Datasource example</description>
<res-ref-name>jdbc/SqlServerDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
不行,一样的错误:你还有办法吗
private DataSource ds = null;
private Context ic = null;
private Connection conn = null;
private Statement stmt = null;
ResultSet rs = null;
ic = new InitialContext();
ds =(DataSource)ic.lookup("此为你配的数据源的名称");
conn = ds.getConnection();