jdbc预编译可以有两种方式:

方式一、jdbc自己实现的预编译,就是做一下特殊字符处理来防SQL注入,看PreparedStatement源码就可以了。

public static void main(String[] args) {

    try {

        final String driverClassName = "com.mysql.jdbc.Driver";
final String url = "jdbc:mysql://10.6.9.14:3306/SBLOG"; 重点看这里
final String username = "sdl";
final String password = "sdl"; Connection connection = DriverManager.getConnection(url2, username, password); String sql = " SELECT *\n" +
" FROM t_web\n" +
" WHERE id = ? and name like ?"; Class.forName(driverClassName); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, 1); preparedStatement.setString(2, "%ing%"); ResultSet rst = preparedStatement.executeQuery(); rst.next(); System.out.println(rst.getString(2));
} catch (Exception e) {
e.printStackTrace();
} } 这里是调用MySQL时的wireshark截图。可以看下实际上就是拼接完成的SQL发过去的。

方式二、利用MySQL的预编译,。

public static void main(String[] args) {

    try {

        final String driverClassName = "com.mysql.jdbc.Driver";
final String url2 = "jdbc:mysql://10.6.8.4:3306/SBLOG?useServerPrepStmts=true"; 重点看这里增加了useServerPrepStmts=true
        final String username = "sdl";
final String password = "sdl"; Connection connection = DriverManager.getConnection(url2, username, password); String sql = " SELECT *\n" +
" FROM t_web\n" +
" WHERE id = ? and name like ?"; Class.forName(driverClassName); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, 1); preparedStatement.setString(2, "%ing%"); ResultSet rst = preparedStatement.executeQuery(); rst.next(); System.out.println(rst.getString(2));
} catch (Exception e) {
e.printStackTrace();
} }
这里是调用MySQL时的wireshark截图。可以看下实际上参数是用的占位符?。

mybatis这种框架也是一样。关键看你的jdbc url怎么配置的,和框架没关系。

jdbc预编译实现方式的更多相关文章

  1. jdbc预编译插入数据操作

    package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepare ...

  2. JDBC预编译语句表名占位异常

    有时候,我们有这样的需求,需要清空多个表的内容,这样我们有两种做法,可用delete from table 或 truncate table table,两种方法视情况而定,前者只是一条条的删除表数据 ...

  3. jdbc预编译

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp20 JAVA_JDBC预编译 相关知识点 什么是预编译语句? 预编译语句P ...

  4. JDBC预编译statement(preparedstatement)和statement的比较、execute与executeUpdate的区别

    和 Statement一样,PreparedStatement也是用来执行sql语句的与创建Statement不同的是,需要根据sql语句创建PreparedStatement除此之外,还能够通过设置 ...

  5. JDBC 预编译语句对象

    Statement的安全问题:Statement的执行其实是直接拼接SQL语句,看成一个整体,然后再一起执行的. String sql = "xxx"; // ? 预先对SQL语句 ...

  6. JDBC 操作预编译语句中LIKE模糊匹配怎么用

    问题描述 在使用JDBC 预编译执行语句时,遇到一个问题,那就是在含有LIKE的查询语句时,我到底怎么使用匹配符%._呢. 如: SELECT * FROM "+LQ_USERS+" ...

  7. mybatis深入理解之 # 与 $ 区别以及 sql 预编译

    mybatis 中使用 sqlMap 进行 sql 查询时,经常需要动态传递参数,例如我们需要根据用户的姓名来筛选用户时,sql 如下: select * from user where name = ...

  8. mybatis之 # 与 $ 区别以及 sql 预编译

    mybatis 中使用 sqlMap 进行 sql 查询时,经常需要动态传递参数,例如我们需要根据用户的姓名来筛选用户时,sql 如下: select * from user where name = ...

  9. Mybatis动态传入tableName--非预编译(STATEMENT)

    在使用Mybatis过程中,你可以体会到它的强大与灵活之处,由衷的为Mybatis之父点上999个赞!在使用过程中经常会遇到这样一种情况,我查询数据的时候,表名称是动态的从程序中传入的,比如我们通过m ...

随机推荐

  1. python27期day14:有参装饰器、多个装饰器装饰一个函数、递归、作业题

    1.有参装饰器:给装饰器添加一个参数.来控制装饰器的行为. @auth(参数) auth里层的函数名 = auth(参数) 被装饰的函数名 = auth里层的函数名(被装饰的函数名) 被装饰的函数名( ...

  2. Artificial Intelligence in Finance

    https://sigmoidal.io/real-applications-of-ai-in-finance/ Artificial Intelligence is taking the finan ...

  3. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  4. MySQL实战45讲学习笔记:第八讲

    一.今日内容概要 我在第 3 篇文章和你讲事务隔离级别的时候提到过,如果是可重复读隔离级别,事务 T 启动的时候会创建一个视图 read-view,之后事务 T 执行期间,即使有其他事务修改了数据,事 ...

  5. [LeetCode] 552. Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  6. Spring Cloud Gateway 之 AddRequestHeader GatewayFilter Factory

    今天我们来学习下GatewayFilter Factory,中文解释就是过滤器工厂. 官方文档对GatewayFilter Factory的介绍: Route filters allow the mo ...

  7. 手把手教你 通过 NuGet.Server 包 搭建nuget服务器,并使用桌面工具上传 nuget 包,免命令行

    新建web项目 工具:VS2013 版本:.Net Framework 4.6,低版本也行,不过要找到对应版本的Nuget.Server 装了NuGet客户端(百度如何安装) WebForm或MVC都 ...

  8. Git基础-第2章

    简单的Git基础概念: repository: 仓库 track:  跟踪 stage: 暂存 commit:    提交 push:        推送 pull:    拉取 一.获取Git仓库 ...

  9. Extra:Cg Math Functions

    常用Cg函数 数学函数 abs(x):绝对值 // float类型的实现 float abs(float x) { return max(-a, a); } sin(x):正弦,输入为弧度 // fl ...

  10. SQL -------- 简单的增删改查

    sql  结构化查询语言,一种ansi 的标准计算机语言,为了访问数据库 可以做什么:可以对数据库 和表进行创建于删除, 对表里面的数据进行增删改查. 也可以创建存储过程和视图,对表设置权限 RDBM ...