当前位置: 首页 > news >正文

商城网站建设net2006优化模型

商城网站建设net2006,优化模型,vue做的pc线上网站,定制东莞网站制作公司场景需求 需要根据不同的列进行对应的排序操作,实现动态列名排序 类似🐟动态查询或更新 但是JDBC预编译模式下占位符的排序字段失效 SQL语句 分页查询 select * from (select t.*, rownum rn from(select * from emp order by empno desc) t where …

场景需求

需要根据不同的列进行对应的排序操作,实现动态列名排序 类似🐟动态查询或更新

但是JDBC预编译模式下占位符的排序字段失效

SQL语句

分页查询

select * from (select t.*, rownum rn from(select * from emp order by empno desc) t where rownum <= 5)    where rn > 0;

指定列排序失效

select * from (select t.*, rownum rn from(select * from emp order by ?  desc) t where rownum <= 5)    where rn > 0;

临时解决字符串拼接

  • SQL注入问题
select * from (select t.*, rownum rn from(select * from emp order by "+ empno  +  "  desc) t where rownum <= 5)    where rn > 0;

预编译注入排序列名排序失效

PreparedStatement执行SQL时,如果order by之后的排序字段使用占位符,通过setString设置值的话,会导致排序失效

// 4 SQL 语句被预编译并存储在 PreparedStatement 对象中。然后可以使用此对象多次高效地执行该语句。
PreparedStatement pstmt = conn.prepareStatement(sql);// 如果SQL有?号  参数需要注入
pstmt.setString(1, sortColumnName); // 1 代表 第一个?  从1开始   以此类推

源码

/*** An object that represents a precompiled SQL statement.* <P>A SQL statement is precompiled and stored in a* {@code PreparedStatement} object. This object can then be used to* efficiently execute this statement multiple times.** <P><B>Note:</B> The setter methods ({@code setShort}, {@code setString},* and so on) for setting IN parameter values* must specify types that are compatible with the defined SQL type of* the input parameter. For instance, if the IN parameter has SQL type* {@code INTEGER}, then the method {@code setInt} should be used.** <p>If arbitrary parameter type conversions are required, the method* {@code setObject} should be used with a target SQL type.* <P>* In the following example of setting a parameter, {@code con} represents* an active connection:* <pre>{@code*   BigDecimal sal = new BigDecimal("153833.00");*   PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES*                                     SET SALARY = ? WHERE ID = ?");*   pstmt.setBigDecimal(1, sal);*   pstmt.setInt(2, 110592);* }</pre>** @see Connection#prepareStatement* @see ResultSet* @since 1.1*/
public interface PreparedStatement extends Statement {/*** Sets the designated parameter to the given Java {@code String} value.* The driver converts this* to an SQL {@code VARCHAR} or {@code LONGVARCHAR} value* (depending on the argument's* size relative to the driver's limits on {@code VARCHAR} values)* when it sends it to the database.** @param parameterIndex the first parameter is 1, the second is 2, ...* @param x the parameter value* @throws SQLException if parameterIndex does not correspond to a parameter* marker in the SQL statement; if a database access error occurs or* this method is called on a closed {@code PreparedStatement}*/void setString(int parameterIndex, String x) throws SQLException;... 
}

核心解释

  • 将指定的参数设置为给定的Java{@code String}值。
  • 驱动程序转换此转换为SQL{@code VARCHAR}或{@code LONGVARCHAR}值

原因

PreparedStatement用占位符防止SQL注入的原理是,在为占位符设置值时,会将值转为字符串,然后转义,再将值放入反引号中,放置在占位符的位置上。

因此,当排序字段使用占位符后,原来的排序语句 order by empno(假设排序字段是empno),在实际执行时变成了 order by empno,根据字段排序变成了根据字符串常量值empno排序,导致排序失效,甚至任意的注入数值都不影响前面的查询结果

情况一

使用预编译的数据库操作对象在order by后面设置占位符,再通过pstmt.setString()方法填入参数会导致排序失败

情况二

使用mybatis的时候,在mapper sql映射.xml文件中,在order by 后面使用 #{参数名} 依然会导致排序失败,因为mybatis #{} 使用的是PrepareStatement

解决办法

  • #{}方式传参数只能处理值参数 不能传递表名,字段等参数
  • ${}字符串替换,可以动态处理表名,字段参数

#{}改成${}, #{}是预编译,相当于PrepareStatement;${}是普通字符串的拼接,相当于Statement

但是必须注意SQL注入的风险,对参数做好校验处理

http://www.hkea.cn/news/93917/

相关文章:

  • 谷歌外贸建站多少钱搭建网站教程
  • 赚钱靠普的网站关键字搜索软件
  • 建设银行深分行圳招聘网站做游戏推广一个月能拿多少钱
  • 北京网站建设及推广招聘关键词排名代做
  • 对网站建设的意见建议网络营销推广的方法有哪些
  • 爬虫网站怎么做怎样才能在百度上面做广告宣传
  • 网站页码南昌做seo的公司有哪些
  • 网络设计方案包括哪些深圳百度推广seo公司
  • 亚马逊跨境电商开店站长工具seo综合查询5g
  • 网站怎么做百度快照logo百度快照优化推广
  • 山西网站建设排名seo技术培训山东
  • 日韩系成人影片成首选网站如何优化推广
  • 网站到期续费通知搜索风云排行榜
  • 网站公司说我们做的网站服务器不够用哪个杭州seo好
  • 类似淘宝网站建设费用杭州哪家seo公司好
  • 装修网站怎样做seo专员很难吗
  • 无锡网站外包如何接广告赚钱
  • 英文网站制作 官网淘宝标题优化网站
  • 电力建设网站网络推广网站的方法
  • 如何做网站窗口网站优化网络推广seo
  • 营销型网站建设效果网络营销策划推广方案
  • 专业的网站搭建多少钱网站seo优化价格
  • 广州公司网站设计制作win10优化大师官网
  • 做调查哪个网站比较可靠百度指数查询
  • 怎么在建设厅网站报名广州网站优化服务
  • 怎么用dw做静态网站b站好看的纪录片免费
  • 济南网站建设那家好网站制作公司有哪些
  • 域名和网站名不一样营销公司
  • discuz做电影网站免费网站seo
  • 惠民建设局网站明年2024年有疫情吗