preparedStatement问号的深入理解

版权声明:报名咨询QQ:3246333637,本文为 张晨光老师 创作,未经博主允许不得转载 https://blog.csdn.net/zhangchen124/article/details/82798623
/**
     * 根据表名查询总条数;
     * @param tableName
     * @return
     */
    public int getCount(String tableName){
        String sql="select count(*) from "+tableName;
        Connection conn=getConnection();
        PreparedStatement pst=null;        
        ResultSet rs=null;
        int count=0;
        try {
            pst = conn.prepareStatement(sql);
            //pst.setString(1,tableName);
            rs=pst.executeQuery();
            if(rs.next())
                count=rs.getInt(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            closeAll(rs, pst, conn);
        }
        return count;
        
    }

总结: 
PreparedStatement只能用来为可以加引号’的参数(如参数值)设置动态参数,即用?占位,不可用于表名、字段名等。

猜你喜欢

转载自blog.csdn.net/zhangchen124/article/details/82798623