我想不通为什么从数据库中去不出记录?
谢谢各位的帮助,终于没有错误提示了。但是为什么会找不到记录呢?离成功不远了,大家再帮我一把!小弟真的不知怎么感谢大家!
下面是javaBean的代码:
package foo;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Test {
String foo = "Not Connected";
int bar = -1;
public void init() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds = (DataSource)ctx.lookup("jdbc/test");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
foo = "Got Connection " + conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery("select * from exam");
if(rst.next()) {
foo=rst.getString("value");
bar=rst.getInt("id");
}
conn.close();
}
}
}//end try
catch(Exception e) {
e.printStackTrace();
}
}//end init()
public String getFoo() {
return foo;
}
public int getBar() {
return bar;
}
}
这是jsp的代码:
<%@
page language = "java"
import = "foo.*"
%>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<%
Test tst = new Test();
tst.init();
%>
<h2>Ms sql server 2000 java search Results</h2>
Foo <%= tst.getFoo() %><br/>
Bar <%= tst.getBar() %>
</body>
</html>
页面显示的是:“Not Connected”和 “-1”,好像init()没有执行,不知是为什么?