struts2中用数据库连接验证登陆问题--------请高手帮忙!!!

songweipeng 2008-10-29 08:17:47
数据库名称:bbs_db 表名:tb_user
jsp页面代码:
[code=JSP]
<h2 align="center">会员登陆</h2>
<form action="login.action" method="post">
<table align="center">
<tr>
<th>用户名:</th>
<td width="80"><input type="text" name="username"/></td>
</tr>
<tr>
<th>密     码:</th>
<td width="80"><input type="password" name="password"/></td>
</tr>

<tr>
<td colspan="2" align="right">
<input type="submit" value="提交"/>
        
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
[/code]
通过strut.xml映射的类为UserLogin.java代码如下:


package com.swp.login;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.opensymphony.xwork2.Action;
import com.swp.datebase.UseDate;

public class UserLogin implements Action{
private String username;
private String password;

public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String execute() throws Exception{
UseDate date = new UseDate();
Statement stmt = null;
ResultSet rs = null;
Connection conn = date.getConn();
System.out.println("nihao");
stmt = conn.createStatement();
//System.out.println("nihao");
String check = "select * from tb_user where username='"+getUsername()+"'and password='"+getPassword()+"'";
rs = stmt.executeQuery(check);
if(rs.next()) {
return SUCCESS;
}else {
return LOGIN;
}
}
}

...全文
107 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wonflay 2008-10-30
  • 打赏
  • 举报
回复
没明白
orz_ORZ_orz 2008-10-30
  • 打赏
  • 举报
回复
这样你都不报个错什么的?
if(rs.next()) {
return SUCCESS;
}else {
return LOGIN;
}

execute根本没有返回值嘛,
return LOGIN;写到 if 外面
APOLLO_TS 2008-10-30
  • 打赏
  • 举报
回复
SUCCESS与LOGIN在那定义的?
难道是带“”;
Landor2004 2008-10-29
  • 打赏
  • 举报
回复
当调用UseDate date = new UseDate(); 的时候
UseDate()构造函数被执行
然而里面的finally 始终被执行,导致conn始终是null

解决办法是在UseDate中另外加一个close方法,然后写finally代码
在action中调用一下close
zhj92lxs 2008-10-29
  • 打赏
  • 举报
回复
楼主去另一个帖子看吧,应该给找出原因了
zhj92lxs 2008-10-29
  • 打赏
  • 举报
回复
怎么了
wj_18 2008-10-29
  • 打赏
  • 举报
回复
LZ想问什么?想要什么效果?

81,092

社区成员

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

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