为什么数据库能连上,但是不能取出数据,麻烦了!

xiaoxiaocainiao110 2006-09-26 06:12:43
建立了一个数据库mydb,再建表username,写入一个数据tanglei,数据类型为varchar,列名为user_name,然后编一个用户登陆的login.JSP,<form action="main.jsp" method="POST">
<p> 用户名:
<input type="text" size="10" name="user_name"/>
</form>
再编一个main.jsp,用
String username=request.getParameter("user_name");
if(username==null){username="";}
byte a[] =username.getBytes("ISO-8859-1");
username=new String(a);
然后连接数据库也正常,用String t=rs.getString("user_name");然后用out,print(t)也可以显示,再写if(username==t){out.print("OK!");}else{out.print("false!")}
运行程序,在login.jsp中输入tanglei提交,但是mian.jsp中总是显示false,实在不懂,麻烦了!!谢谢!
...全文
239 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinyantao 2006-09-28
  • 打赏
  • 举报
回复
if(username==null || username.equals("")){
...
}
cocoysy 2006-09-28
  • 打赏
  • 举报
回复
if(username==t)
{
out.print("OK!");
}
else
{
out.print("false!")
}
改为
if(username.equals(t))
{
out.print("OK!");
}
else
{
out.print("false!")
}

xiaoxiaocainiao110 2006-09-28
  • 打赏
  • 举报
回复
<%@page contentType="text/html; charset=GB2312"%>
<%@page import="java.sql.*"%>
<html>
<head></head>
<body bgcolor="#ffffc0">
<form action="" method="POST">
<p> 用户名:
<input type="text" size="10" name="username"/>
<p><input type="submit" value="提交"/>
</form>
<%--获得用户信息 --%>
<%
String user=request.getParameter("username");
if(user==null|| user.length()==0){user="";}
byte a[] =user.getBytes("ISO8859_1");
user=new String(a);
%>
<%-- 连接数据库!--%>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e)
{}
ResultSet rs=null;
try {
Connection con =DriverManager.getConnection("jdbc:odbc:sun","sa","123");
Statement sql=con.createStatement();
String condition ="select * from user_name";
rs =sql.executeQuery(condition);
}
catch (Exception ex) {}
%>
<%--检验输入的用户名是否正确! --%>
<%
try{
while(rs.next())
{
String t=rs.getString("username");
if(t.equals(user)){
out.print("OK!");
}
else
{out.print("NO!");}
}
}
catch(Exception e)
{System.out.print("验证有问题"); }

%>
</body>
</html>
数据库中数据类型为nvarchar,不知道和问题有没有 关系,
第一次提问就有这么多兄弟们顶真是谢谢了,帮我 分析一下错误原因吧,好吗?
kinglz 2006-09-27
  • 打赏
  • 举报
回复
t.equals(username)
likgui 2006-09-27
  • 打赏
  • 举报
回复
基础要紧!!!
tigerwithwing 2006-09-27
  • 打赏
  • 举报
回复
同意equals
Hao粑粑爱生活 2006-09-27
  • 打赏
  • 举报
回复
JAVA基础再多看看吧
barbarianli 2006-09-27
  • 打赏
  • 举报
回复
字符串比较应该用equals
不要用==

正解
xueguiping 2006-09-27
  • 打赏
  • 举报
回复
up
nina36633640 2006-09-27
  • 打赏
  • 举报
回复
t.equals(username)
脚本中使用==
程序片中字符串的比较应该使用equals
marine_chen 2006-09-26
  • 打赏
  • 举报
回复
username==null
改成
(username == null || username.length()==0)
试试
vacuumboy 2006-09-26
  • 打赏
  • 举报
回复
字符串比较应该用equals
不要用==

81,092

社区成员

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

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