我想不通为什么从数据库中去不出记录?

wanglei810522 wanglei810522 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
谢谢各位的帮助,终于没有错误提示了。但是为什么会找不到记录呢?离成功不远了,大家再帮我一把!小弟真的不知怎么感谢大家!

下面是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()没有执行,不知是为什么?
...全文

相关推荐

评论 13
收藏 已收藏
分享
返回顶部
wanglei810522
Web 开发 81,108 成员 341,721 内容