java PreparedStatement ResultSet 的数据库问题

qq_35012542 2017-09-23 03:36:13
//显示职工表所有记录
public List<person> Search() throws SQLException{
Connection conn = sjk2.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select Eno,Cno,En,Sex,Etitle,Eage,Esal from zhigong");
List<person> people = new ArrayList<person>();
person p = null;
while(rs.next()){//如果对象中有数据,就会循环打印出来
p = new person();
p.setEno(rs.getString("Eno"));
p.setCno(rs.getString("Cno"));
p.setEn(rs.getString("En"));
p.setSex(rs.getString("Sex"));
p.setEtitle(rs.getString("Etitle"));
p.setEage(rs.getInt("Eage"));
p.setEsal(rs.getInt("Esal"));

people.add(p);
}
return people;

}


目前的问题是,我想单独查询表中符合某条件 带参数(“Eno=?”)的数据集
想替换 加这一句("select Eno,Cno,En,Sex,Etitle,Eage,Esal from zhigong where Eno=?") 加上去后系统提示语法有错误
请问应该怎么修改

这个是带参数的另一个 可以运行,主要问题是不会结合ResultSet 求大神指导 谢谢~
//delete
public void SearchOne(String Eno) throws SQLException{
person p = null;
Connection conn=sjk2.getConnection();
String sql="" +
"delete from zhigong where Eno = ?";
PreparedStatement psmt = conn.prepareStatement(sql);
psmt.setString(1,Eno);

//执行SQL语句
psmt.execute();

}
...全文
392 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
风不会止息 2017-09-23
  • 打赏
  • 举报
回复
要用预编译prestatament对象,用占位符
qq_35012542 2017-09-23
  • 打赏
  • 举报
回复
//search List<person> people = new ArrayList<person>(); people = p.Search1(); for(person person : people){ String str = person.getEno()+"," +person.getCno()+"," +person.getEn()+"," +person.getSex()+"," +person.getEtitle()+"," +person.getEage()+"," +person.getEsal(); System.out.println(str); 这是查询语句 如果我把问号改成 具体的一个数值 就可以运行"select Eno,Cno,En,Sex,Etitle,Eage,Esal from zhigong where Eno= 152501" 但是改成变量就没有结果了
woshiyidaocai 2017-09-23
  • 打赏
  • 举报
回复
" ... where Eno=?" 改成 " ... where Eno = ' " + name +" ' " 试试 就是用单引号将值括起来 ,数字就不用了

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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