preparedstatement 执行查询语句有误

J2EE 码拜 8年前 (2016-09-11) 1343次浏览
这个是dao层的代码。

public class userdao {
	private Connection connection =null; 
	private PreparedStatement ps =null;
	//@SuppressWarnings("null")
	public User query(String username) throws Exception{
		ResultSet rs = null;
		//String username = user.getUsername();
		//String password = user.getPassword();
		String sql = "select * from user where username =?";
		connection=DB.getConnection();
		ps = DB.prepared(connection, sql);
		ps.setString(1, username);
		rs=ps.executeQuery();
		if(rs==null){
			return null;
		}
		User user = new User();
		while(rs.next())			 
		{
				user.setUsername(rs.getString(1));
				user.setPassword(rs.getString(2));
					//System.out.println("目前用户为"+user);
		 	}
		rs.close();
		ps.close();
		connection.close();
		return user;
		}
	/*	else{
			return null;
		}*/
	public User add(User user) throws Exception{
		//if(query(user.getUsername())==null){
		connection = DB.getConnection();
		String sql = "insert into user (username,password) values(?,?)";
		ps = DB.prepared(connection, sql);
		ps.setString(1, user.getUsername());
		ps.setString(2, user.getPassword());
		ps.executeUpdate();
		ps.close();
		connection.close();
		//}//else{
		//	return null;
		//}
		return user;


	}
}

这个是service的代码。

public class UserService {
	private userdao daoUserdao = new userdao();
	public User login(String username,String password) throws Exception{
		User user = daoUserdao.query(username);
		if(user==null){
			throw new Exception("用户名错误!");
		}
		if(!password.equals(user.getPassword())){
			throw new Exception("密码错误!");
		}
		return user;
	}
	public void regist(User user)throws Exception{

		User _user = daoUserdao.query(user.getUsername());
		if(_user!=null){
			throw new Exception("用户名已经被注册!");
		}else{
		  daoUserdao.add(user);
		}
	}
}

输入任何一句注册,都显示已经被注册。
preparedstatement 执行查询语句有误preparedstatement 执行查询语句有误

解决方案

10

请把这个是dao层的代码de 17行写到while{  }循环中。你这样创建对象会覆盖掉前面的。

30

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.sun.entity.User
import com.sun.DBUtil
//DBUtil增删改的通用方法
{
/**
* 更新通用的方法
* @param sql(update/insert/delete)
* @param pramValue(传入参数,没有设为null)
*/
public static int update(String sql,Object[] prams){
Connection conn=null;
PreparedStatement pst=null;
int num=0;
try {
//创建链接
conn=getConnection();
//创建执行命令pst对象
pst=conn.prepareStatement(sql);
//设置元参数,得到占位符的个数
int count=pst.getParameterMetaData().getParameterCount();
//设置占位符参数的值
if(prams!=null&&prams.length>0){
//循环给参数赋值
for(int i=0;i<count;i++){
pst.setObject(i+1, prams[i]);
}
}
//执行更新
num=pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
close(conn, pst);
}
return num;
}
/**
* 动态赋值
* @param pst
* @param prams
*/
public static void setPrams(PreparedStatement pst,Object[] prams){
//判断prams能否为空
if(prams==null) {
return;
}
//循环赋值
for(int i=0;i<prams.length;i++){
try {
pst.setObject(i+1,prams[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
class Dao{
public boolean add(User u) {
/**
* 添加
*/
String sql=”insert into newsauthor values(PERSON_SEQUENCE.nextval,?,?)”;
Object[] prams={u.getName(),u.getPwd()};
int f=DBUtil.update(sql, prams);
if(f>0){
return true;
}
return false;
}
/**
* 查询
*/
public User selectUser(String name, String pwd) {
User u=null;
conn=DBUtil.getConnection();
try {
String sql=”select * from newsauthor where aname=? and apwd=?”;
pst=conn.prepareStatement(sql);
Object [] prams={name,pwd};
DBUtil.setPrams(pst, prams);
rs=pst.executeQuery();
if(rs.next()){
u=new User();
u.setUserid(rs.getInt(“auid”));
u.setName(rs.getString(“aname”));
u.setPwd(rs.getString(“apwd”));
}
} catch (SQLException e) {
e.printStackTrace();
}
return u;
}
}
service 你就可以本人写了


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明preparedstatement 执行查询语句有误
喜欢 (0)
[1034331897@qq.com]
分享 (0)