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语句出错

解决方案 »

  1.   

    SqlHelper.javapackage jdbc1;import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.*;
    import java.util.Properties;public class SqlHelper { // =======定义需要的变量=========
    private static Connection con = null;
    //在大多数情况下,我们使用的是preparedstatement来替代statement
    private static PreparedStatement ps=null;
    private static ResultSet rst = null; // 连接数据库参数
    static String url = "";
    static String username = "";
    static String driver = ""; static Properties pp = null;
    static FileInputStream files = null; static{
    try {
    // 从dbinfo文件中读取配置信息
    pp = new Properties();
    files = new FileInputStream("dbinfo.properties");
    pp.load(files);
    url = pp.getProperty("url");
    driver = pp.getProperty("driver"); Class.forName(url); }  catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    files=null;
    }
    // 得到链接
    public static Connection getCon(){
    try   

    con = DriverManager.getConnection(driver);
    }catch(Exception e){
    e.printStackTrace();
    }
    return con; 
    }

    //=============先写一个Update /delete / insert=======
    //sql格式:update 表名 set ziduanming=? where ziduan=?
    public void executeUpdate(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.executeUpdate();
    }catch(Exception e){
    e.printStackTrace();//开发阶段
    //抛出异常
    throw new RuntimeException(e.getMessage());
    }
    }

    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;
    }
    }
      

  2.   


    JdbcTest1.javapackage jdbc1;import java.sql.ResultSet;
    import java.sql.SQLException;import org.junit.*;public class JdbcTest1 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    //测试sqlhelper是否能正常使用


    }
    @Test
    public void testSql() throws SQLException {

    String sql="select * from ?";
        ResultSet rst=SqlHelper.select(sql,"Product");
        while(rst.next()){
        System.out.println(rst.getString(1));
        }

    }}
      

  3.   

    问号只能在表对应的列上,其他地方要自己拼接sql语句
      

  4.   

    public void testSql() throws SQLException {
            
            String sql="select * from Product";
            ResultSet rst=SqlHelper.select(sql,"");
            while(rst.next()){
            System.out.println(rst.getString(1));
            }
            
        }
    写成这样子试试 ?
      

  5.   

    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;
        }
    修改成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;
        }
      

  6.   

    楼主真是让我大开眼界啊。
    从来没那样写过,不过看你的错误,a table name is required,那应该是不可以了,找不到表名。
      

  7.   

    ?只能对参数赋值,不能作为本身传递给sql。所以想:select ? from ?这样的同学注意了。肯定会报错的,找不到列,找不到表。
      

  8.   

    应该只能在where子句里用占位符
      

  9.   

    ?只能对参数赋值,不能作为本身传递给sql。所以想:select ? from ?这样的同学注意了。肯定会报错的,找不到列,找不到表。
      

  10.   

    我晕,?号是当参数使的,所有sql里面的关键字,包括表名,列名其他等等都不能当?号传的。说白了,就是把?的内容当作一个完整的字符串来处理,以防止sql注入。你觉得
    select * from 'Product';
    可以执行么
      

  11.   

    我明白了就是说table名和关键字不能用这个问号