连接数据库问题?

szm629 2004-02-18 10:27:21
JavaBean单独测试时可输出数据库内容,但从Jsp中调用JavaBean时Rs为空,帮忙看一下;
1。JavaBean
package WebMail;
import java.sql.*;
public class conn
{
Connection connect=null;
ResultSet rs=null;
Statement stmt=null;
public conn()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
connect=DriverManager.getConnection("jdbc:odbc:WebMail","sa","");
stmt=connect.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
String col1=rs.getString(2);
String col2=rs.getString("password");
System.out.print(col1);
System.out.print(col2);
}
}
catch(SQLException ex)
{
System.err.println(ex.getMessage());
}
return rs;
}
/* public static void main(String[] areg)
{
conn n=new conn();
n.executeQuery();

}*/
}
2。
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.sql.*" %>
<html>
<body>
<jsp:useBean id="userBean" class="WebMail.conn" scope="page">
</jsp:useBean>
<table width="786" height="156" border="0" align="center">
<!--DWLayoutTable-->
<tr>
<td width="776" height="20" bgcolor="#0099FF">
<div align="center">成员登陆</div></td>
</tr>
<tr>
<td height="134">
<form name="form1" method="post" action="login_ok.jsp">
<p align="center">名 字:
<input name="logname" type="text" id="logname">
</p>
<p align="center">密 码:
<input name="logpass" type="password" id="logpass">
</p>
<p align="center">| 密码忘了,找一下 | 注册新用户 |
<input name="login" type="submit" id="login" value="登陆">
</p>
</form></td>
</tr>
</table>
<%!
public String getStr(String str)
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e){}
return null;
}
%>
<%!
String logname,logpass;
boolean loginAttempt=false;
boolean loginOK=false;
String errorMessage="请您登陆";
%>
<%
if(request.getParameterValues("login")!=null
&&request.getParameterValues("logname")!=null
&&request.getParameterValues("logpass")!=null)
{
loginAttempt=true;
}
if(loginAttempt)
{
logname=request.getParameter("logname");
logpass=request.getParameter("logpass");
logname=getStr(logname);
logpass=getStr(logpass);
String sql="select * from member where logname='"+logname+"'";
ResultSet RS=userBean.executeQuery(sql);//估计问题是userBean.executeQuery(sql)这一句
int rowscount=0;
try
{
while(RS.next())
{
rowscount++;
}
}
catch(Exception e){}
if(rowscount!=0)
{
errorMessage="成功登陆";
session.setAttribute("username",logname);
loginOK=true;
if(loginOK)
{out.println("ssdfsdf");
response.sendRedirect("main.jsp");
}
}
else
{
errorMessage="您的用户名或密码不正确";
session.setAttribute("username","");
out.print("<center>");
out.print("<table>");
out.print("<tr>");
out.print("<td>"+errorMessage+"</td>");
out.print("</tr>");
out.print("</table>");
out.print("</center>");out.print(rowscount);
out.print(RS);
}
}
%>
</body>
</html>
估计问题是userBean.executeQuery(sql)这一句
...全文
26 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复

81,092

社区成员

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

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