日期:2014-05-20  浏览次数:20784 次

jdbc的问题, sql语句出错
Java code
    String sql="select * from ?";
        ResultSet rst=SqlHelper.select(sql,"Product");
        while(rst.next()){
        System.out.println(rst.getString(1));
        }



[Microsoft][ODBC Microsoft Access Driver] Parameter 'Pa_RaM000' specified where a table name is required.

这个问号的地方写的有问题吗???

我换了别的方法也是现实sql语句出错

------解决方案--------------------
public void testSql() throws SQLException {

String sql="select * from Product";
ResultSet rst=SqlHelper.select(sql,"");
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(parameters!=null){
// for(int i=0;i<parameters.length;i++){
// ps.setString(i+1,parameters[i]);
// }
// }
ps.setString(1,parameters);

rst=ps.executeQuery();
}catch(Exception e){
e.printStackTrace();//开发阶段
//抛出异常
throw new RuntimeException(e.getMessage());
}
return rst;
}
修改成
Java code

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 ?这样的同学注意了。肯定会报错的,找不到列,找不到表。