【急,求助】struts+jdbc查找出现的问题
我的dao包如下:public User findUser(String loginName, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JDBC_utils.getConnection();
String sql = "select * from user where name=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, loginName);
ps.setString(2, password);
rs = ps.executeQuery();
while (rs.next()) {
user = mappingUser(rs);
}
System.out.println("进行查找");
} catch (SQLException e) {
throw new DaoException(e.getMessage(), e);
} finally {
JDBC_utils.free(rs, ps, conn);
}
return user;
}
LoginAction如下
User user=new User();
UserDaoImpl udi=new UserDaoImpl();
/* public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
*/
public String execute()
{
if(udi.findUser(user.getName(),user.getPassword())!=null)
return "welcome";
else
return "regist";
}
为什么总是显示查找不到!!!
急急急,求大神指点~ --------------------编程问答-------------------- 没看到你的 get set 方法,希望截图截全面点。 --------------------编程问答-------------------- 一步一步找问题
页面操作之后有没有进execute这个方法,有没有取到user
数据库能不能连上,数据库里有没值
--------------------编程问答--------------------
package Action;
import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
User user=new User();
UserDaoImpl udi=new UserDaoImpl();
/* public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
*/
public String execute()
{
if(udi.findUser(user.getName(),user.getPassword())!=null)
return "welcome";
else
return "regist";
}
}
user 的get、set注释掉了,用get、set的时候,也无法查询.
User的代码:
package jdbc.user;
import java.util.Date;
//定义了user类
public class User {
private int id;
private String name;
private String password;
private Date birthday;
private float workyear;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public float getWorkyear() {
return workyear;
}
public void setWorkyear(float workyear) {
this.workyear = workyear;
}
}
mysql是连接上的:
返回的信息是:
Unexpected Exception caught setting 'denglu' on 'class Action.LoginAction: Error setting expression 'denglu' with value ['??????', ]
十一月 03, 2013 5:08:21 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'username' on 'class Action.LoginAction: Error setting expression 'username' with value ['lisi', ]
十一月 03, 2013 5:08:21 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'userpass' on 'class Action.LoginAction: Error setting expression 'userpass' with value ['sd', ]
进行查找
struts中的代码是:
<?xml version="1.0" encoding="UTF-8" ?>--------------------编程问答--------------------
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="action" extends="struts-default" >
<!--包的名称 主要供继承者使用 namespace会作为action访问的一部分,简化代码 -->
<action name="LoginAction" class="Action.LoginAction" method="execute">
<result name="welcome">/welcome.jsp</result>
<result name="regist">/regist.jsp</result>
</action>
<action name="RegistAction" class="Action.RegistAction" method="execute">
<result name="success">/Login.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
已经检查很多次了。。。
不知道怎样测试数据库是不是链接上的,我把更详细的代码写在3楼,求指导~ --------------------编程问答-------------------- set get方法是必须的不能注释掉
会debug的话就在关键的地方打个断点看一下各个变量的值
不会的话就将变量的值输出一下,这样就能大概知道问题出在哪里。 --------------------编程问答-------------------- Unexpected Exception caught setting 'userpass' on 'class Action.LoginAction: Error setting expression 'userpass' with value ['sd', ]这个是什么意思 'sd'后面的逗号是不是表示数据格式不对? --------------------编程问答-------------------- 不是很清楚mysql,不知道你的表名叫user会不会有问题,另外可以在这句:
while (rs.next()) {
user = mappingUser(rs);
}
里面把rs.getInt("id")打印出来,应该说只要进入while循环里面就说明查找到内容了。 --------------------编程问答--------------------
并没有查找到
我把struts中的<constant name="struts.devMode" value="true" />设置为true之后
就出现了以下信息反馈。
Unexpected Exception caught setting 'denglu' on 'class Action.LoginAction: Error setting expression 'denglu' with value ['??????', ]十一月 03, 2013 8:02:16 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'username' on 'class Action.LoginAction: Error setting expression 'username' with value ['lisi', ]
十一月 03, 2013 8:02:16 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'userpass' on 'class Action.LoginAction: Error setting expression 'userpass' with value ['123', ]
绿色标记的部分不知道为啥出现啊~ --------------------编程问答--------------------
Error setting expression 'userpass' with value ['123', ],往user对象里面设值的时候报错了吧。不知道你mappingUser这个方法是什么样的,感觉应该是里面出了问题,比如,类型不对。 --------------------编程问答--------------------
不是很清楚mysql,不知道你的表名叫user会不会有问题,另外可以在这句:
while (rs.next()) {
user = mappingUser(rs);
}
里面把rs.getInt("id")打印出来,应该说只要进入while循环里面就说明查找到内容了。
并没有查找到
我把struts中的<constant name="struts.devMode" value="true" />设置为true之后
就出现了以下信息反馈。
Unexpected Exception caught setting 'denglu' on 'class Action.LoginAction: Error setting expression 'denglu' with value ['??????', ]十一月 03, 2013 8:02:16 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'username' on 'class Action.LoginAction: Error setting expression 'username' with value ['lisi', ]
十一月 03, 2013 8:02:16 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
SEVERE: Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'userpass' on 'class Action.LoginAction: Error setting expression 'userpass' with value ['123', ]
绿色标记的部分不知道为啥出现啊~
Error setting expression 'userpass' with value ['123', ],往user对象里面设值的时候报错了吧。不知道你mappingUser这个方法是什么样的,感觉应该是里面出了问题,比如,类型不对。
private User mappingUser(ResultSet rs) throws SQLException {--------------------编程问答-------------------- 你怎么接收前台传的值的,既没看到你有username这个属性,也没看到你实现了Modeldriven<User>接口,而且你的User对象也没有setUsername这个方法。希望对你有帮助,我也有点晕! --------------------编程问答-------------------- 前台传过来的值是不是乱码?????
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setBirthday(rs.getDate("birthday"));
user.setWorkyear(rs.getFloat("workyear"));
user.setPassword(rs.getString("password"));
return user;
}
补充:Java , Java EE