我在其他stackoverflow问题中查找了这个错误,但基本上给出的解决方案与我的代码相同,我不知道我做错了什么。
public List<ProjectUser> findProjectUserId(int id){
List <ProjectUser> users= new ArrayList<>();
ProjectUser projectuser= null;
try {
String req="select * from projectuser where project=?";
PreparedStatement ps= cnx.getConnection().prepareStatement(req);
ResultSet rs= ps.executeQuery();
ps.setInt(1,id);
while (rs.next()){
projectuser= new ProjectUser(rs.getInt(1), rs.getInt(2), rs.getInt(3));
users.add(projectuser);
}
} catch (SQLException ex){
ex.printStackTrace();
}
return users;
}
答案 0 :(得分:1)
设置Paramettres后应该执行Stetement:
IF MOD(counter,2) = 0 THEN
//first_number = counter/2
//second_number = first_number+1
//avg= (first_number+second_number)/2
ELSE
//first_number = counter/2;
//avg = first_number+1
END IF;
答案 1 :(得分:0)
您正在尝试在执行查询之后而不是之前设置绑定值。翻转订单,你应该没问题:
PreparedStatement ps = cnx.getConnection().prepareStatement(req);
ps.setInt(1,id);
ResultSet rs = ps.executeQuery();
答案 2 :(得分:0)
在设置PreparedStatement的参数之前,您正在执行查询。这就是你的代码给出异常的原因。陈述的顺序应为
ps.setInt(1,id);
ResultSet rs= ps.executeQuery();