为什么执行后页面总是显示为“null”?

chelonblue 2010-01-09 05:50:01
ODBC数据源测试成功
===============================================
DatabaseCon.java
package test.db;
//导入所需包
import java.io.*;
import java.sql.*;
public class DatabaseCon{
//声明类成员
public Connection con;
public String URL="jdbc:odbc:order";
public String Message;
public boolean connection(){
try{
//加载JDBC-ODBC桥驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ex){
Message=ex.getMessage();
System.exit(-1);
}
try{
//试图通过JDBC-ODBC桥建立一个与order数据源的连接
con=DriverManager.getConnection(URL);
//数据库连接成功
return true;
}
catch(SQLException ex){
Message=ex.getMessage();
return false;
}
}
//关闭数据库连接
public boolean close(){
try{
con.close();
return true;
}
catch(Exception ex){
Message=ex.getMessage();
return false;
}
}
//执行SQL查询语句,返回执行结果集
public ResultSet exeSQL(String sqlstr){
try{
this.connection();
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sqlstr);
return rs;
}catch(Exception ex){
Message=ex.getMessage();
}
return null;
}
//从指定的结果集rs中返回某字段str的值
public String getString(ResultSet rs,String str){
try{
return rs.getString(str);
}catch(Exception ex){
Message=ex.getMessage();
}
return null;
}
}
================================
testBean.jsp
<%@ page contentType="text/html; charset=gb2312" import="java.sql.*" %>
<html>
<head><title>测试数据库连接Bean</title></head>
<jsp:useBean id="dbBean" scope="session" class="test.db.DatabaseCon"/>
<body>
<%
ResultSet rs=dbBean.exeSQL("select * from admin");
String str;
try{
while(rs.next()){
str=rs.getString("Admin_id");
%>
<%=rs.getString("Admin_Name")%>-> <%=str%> <br>
%>
<%
}}
catch(Exception ex){
out.print(ex.getMessage());
}
%>
</body>
</html>
================================================
试了很多方法,还是没用,求高手解答~
...全文
259 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chelonblue 2010-01-11
  • 打赏
  • 举报
回复
自己解决了 原来是DSN需要系统DSN
crazylaa 2010-01-09
  • 打赏
  • 举报
回复
数据库连接成功了没有?看你这没有用户名,密码啊
另外取数据时,必须先rs.next()才能到游标的第一条记录。
chelonblue 2010-01-09
  • 打赏
  • 举报
回复
没其他原因了吗?(在线等)
hongjn 2010-01-09
  • 打赏
  • 举报
回复
说真的 代码可读性很差
hongjn 2010-01-09
  • 打赏
  • 举报
回复
数据库连接成功了吗?
阿_布 2010-01-09
  • 打赏
  • 举报
回复
检查一下字段名是否正确,数据库有没有数据。
chelonblue 2010-01-09
  • 打赏
  • 举报
回复
还是一样诶
changshoujun 2010-01-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoupuyue 的回复:]
<%
ResultSet rs=dbBean.exeSQL("select * from admin");
String str;
try{
while(rs.next()){
str=rs.getString("Admin_id");
%>
<%=rs.getString("Admin_Name")%>-> <%=str%> <br> 
<%
}}
catch(Exception ex){
out.print(ex.getMessage());
}
%>
[/Quote]
换字段看看~
阿_布 2010-01-09
  • 打赏
  • 举报
回复
<%
ResultSet rs=dbBean.exeSQL("select * from admin");
String str;
try{
while(rs.next()){
str=rs.getString("Admin_id");
%>
<%=rs.getString("Admin_Name")%>-> <%=str%> <br>
<%
}}
catch(Exception ex){
out.print(ex.getMessage());
}
%>
阿_布 2010-01-09
  • 打赏
  • 举报
回复

public String getString(ResultSet rs,String str){
try{
if(rs.next())
return rs.getString(str);
}catch(Exception ex){
Message=ex.getMessage();
}
return null;
}
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧