数据库名称: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>密 &nbsp;&nbsp;&nbsp;&nbsp;码:</th>
<td width="80"><input type="password" name="password"/></td>
</tr><tr>
<td colspan="2" align="right">
<input type="submit" value="提交"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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;
}
}
}

解决方案 »

  1.   

    当调用UseDate date = new UseDate(); 的时候
    UseDate()构造函数被执行
    然而里面的finally 始终被执行,导致conn始终是null 解决办法是在UseDate中另外加一个close方法,然后写finally代码
    在action中调用一下close
      

  2.   

    SUCCESS与LOGIN在那定义的?
    难道是带“”;
      

  3.   

    这样你都不报个错什么的?
    if(rs.next()) {
                return SUCCESS;
            }else {
                return LOGIN;
            }execute根本没有返回值嘛,
    return LOGIN;写到 if 外面