日期:2014-05-20 浏览次数:21009 次
String sql="select * from ?";
ResultSet rst=SqlHelper.select(sql,"Product");
while(rst.next()){
System.out.println(rst.getString(1));
}
public static ResultSet select(String sql,String parameters){
try{con=getCon();
ps=con.prepareStatement(sql);
//给问号赋值
if (!"".equals(parameters))
ps.setString(1,parameters);
rst=ps.executeQuery();
}catch(Exception e){
e.printStackTrace();//开发阶段
//抛出异常
throw new RuntimeException(e.getMessage());
}
return rst;
}
------解决方案--------------------
这样写?代替表,压根就找不到表面,你得指定一下表名试试看
------解决方案--------------------
?只能对参数赋值,不能作为本身传递给sql。所以想:select ? from ?这样的同学注意了。肯定会报错的,找不到列,找不到表。