【急,求助】struts+jdbc查找出现的问题

sdauwcy 2013-11-03 05:12:35
我的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";
}


为什么总是显示查找不到!!!

急急急,求大神指点~
...全文
184 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
悲催八零后 2013-11-03
  • 打赏
  • 举报
回复
你怎么接收前台传的值的,既没看到你有username这个属性,也没看到你实现了Modeldriven<User>接口,而且你的User对象也没有setUsername这个方法。希望对你有帮助,我也有点晕!
sdauwcy 2013-11-03
  • 打赏
  • 举报
回复
引用 9 楼 acodemaker 的回复:
[quote=引用 8 楼 sdauwcy 的回复:] [quote=引用 7 楼 acodemaker 的回复:] 不是很清楚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', ] 绿色标记的部分不知道为啥出现啊~[/quote] Error setting expression 'userpass' with value ['123', ],往user对象里面设值的时候报错了吧。不知道你mappingUser这个方法是什么样的,感觉应该是里面出了问题,比如,类型不对。[/quote]
private User mappingUser(ResultSet rs) throws SQLException {
		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;
	}
悲催八零后 2013-11-03
  • 打赏
  • 举报
回复
引用 8 楼 sdauwcy 的回复:
[quote=引用 7 楼 acodemaker 的回复:] 不是很清楚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', ] 绿色标记的部分不知道为啥出现啊~[/quote] Error setting expression 'userpass' with value ['123', ],往user对象里面设值的时候报错了吧。不知道你mappingUser这个方法是什么样的,感觉应该是里面出了问题,比如,类型不对。
sdauwcy 2013-11-03
  • 打赏
  • 举报
回复
引用 7 楼 acodemaker 的回复:
不是很清楚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', ] 绿色标记的部分不知道为啥出现啊~
悲催八零后 2013-11-03
  • 打赏
  • 举报
回复
不是很清楚mysql,不知道你的表名叫user会不会有问题,另外可以在这句: while (rs.next()) { user = mappingUser(rs); } 里面把rs.getInt("id")打印出来,应该说只要进入while循环里面就说明查找到内容了。
sdauwcy 2013-11-03
  • 打赏
  • 举报
回复
引用 5 楼 udbwcso 的回复:
set get方法是必须的不能注释掉 会debug的话就在关键的地方打个断点看一下各个变量的值 不会的话就将变量的值输出一下,这样就能大概知道问题出在哪里。
Unexpected Exception caught setting 'userpass' on 'class Action.LoginAction: Error setting expression 'userpass' with value ['sd', ]这个是什么意思 'sd'后面的逗号是不是表示数据格式不对?
尘缘udbwcso 2013-11-03
  • 打赏
  • 举报
回复
set get方法是必须的不能注释掉 会debug的话就在关键的地方打个断点看一下各个变量的值 不会的话就将变量的值输出一下,这样就能大概知道问题出在哪里。
sdauwcy 2013-11-03
  • 打赏
  • 举报
回复
引用 2 楼 udbwcso 的回复:
一步一步找问题 页面操作之后有没有进execute这个方法,有没有取到user 数据库能不能连上,数据库里有没值
已经检查很多次了。。。 不知道怎样测试数据库是不是链接上的,我把更详细的代码写在3楼,求指导~
sdauwcy 2013-11-03
  • 打赏
  • 举报
回复
引用 1 楼 u010131956 的回复:
没看到你的 get set 方法,希望截图截全面点。
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>
尘缘udbwcso 2013-11-03
  • 打赏
  • 举报
回复
一步一步找问题 页面操作之后有没有进execute这个方法,有没有取到user 数据库能不能连上,数据库里有没值
好名字给猪了 2013-11-03
  • 打赏
  • 举报
回复
没看到你的 get set 方法,希望截图截全面点。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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