我编写了一个测试文件如下 :
<%@ page contentType="text/html;charset=GBK"%><%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>
<html>
<head>
</head>
<body>
<%
// 数据库操作
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
try
{
ctx=new InitialContext();
if(ctx==null)
throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sjk");
if(ds==null)
throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
/*
rs=stmt.executeQuery("select * from mima"); while(rs.next())
{
out.println(rs.getString(2)+"<br>");
}
*/
}
finally
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(cnn!=null)
cnn.close();
if(ctx!=null)
ctx.close();
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=GBK"%><%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>
<html>
<head>
</head>
<body>
<%
// 数据库操作
Context ctx=null;
Connection cnn=null;
Statement stmt=null;
ResultSet rs=null;
try
{
ctx=new InitialContext();
if(ctx==null)
throw new Exception("没有匹配的环境");
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/sjk");
if(ds==null)
throw new Exception("没有匹配数据库");
cnn=ds.getConnection();
stmt=cnn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
/*
rs=stmt.executeQuery("select * from mima"); while(rs.next())
{
out.println(rs.getString(2)+"<br>");
}
*/
}
finally
{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(cnn!=null)
cnn.close();
if(ctx!=null)
ctx.close();
}
%>
</body>
</html>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货