java sql 查询中的转义序列不对_在 JDBC 中使用 SQL 转义序列 - SQL Server

2023-11-18

使用 SQL 转义序列Using SQL escape sequences

08/12/2019

本文内容

按照 JDBC API 的定义,Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server 支持使用 SQL 转义序列。The Microsoft JDBC Driver for SQL ServerMicrosoft JDBC Driver for SQL Server supports the use of SQL escape sequences, as defined by the JDBC API. 转义序列用于 SQL 语句内,以告诉驱动程序应以不同的方式处理 SQL 字符串的转义部分。Escape sequences are used within an SQL statement to tell the driver that the escaped part of the SQL string should be handled differently. 当 JDBC 驱动程序处理 SQL 字符串的转义部分时,它会将字符串的这一部分转换为 SQL Server 可以理解的 SQL 代码。When the JDBC driver processes the escaped part of an SQL string, it translates that part of the string into SQL code that SQL Server understands.

JDBC API 需要五种类型的转义序列,JDBC 驱动程序支持所有这些转义序列:There are five types of escape sequences that the JDBC API requires, and all are supported by the JDBC driver:

LIKE 通配符文本LIKE wildcard literals

函数处理Function handling

日期和时间文本Date and time literals

存储过程调用Stored procedure calls

外部联接Outer joins

限制转义语法Limit escape syntax

JDBC 驱动程序使用的转义序列语法如下所示:The escape sequence syntax used by the JDBC driver is the following:

{keyword ...parameters...}

备注

SQL 转义处理对于 JDBC 驱动程序始终是打开的。SQL escape processing is always turned on for the JDBC driver.

以下各部分介绍五种类型的转义序列以及 JDBC 驱动程序如何支持它们。The following sections describe the five types of escape sequences and how they are supported by the JDBC driver.

LIKE 通配符文本LIKE wildcard literals

JDBC 驱动程序支持 {escape 'escape character'} 语法,以便将 LIKE 子句通配符用作文本。The JDBC driver supports the {escape 'escape character'} syntax for using LIKE clause wildcards as literals. 例如,以下代码将返回 col3 的值,其中 col2 的值实际上以下划线开始(而不是对其使用通配符)。For example, the following code will return values for col3, where the value of col2 literally begins with an underscore (and not its wildcard usage).

ResultSet rst = stmt.executeQuery("SELECT col3 FROM test1 WHERE col2

LIKE '\\_%' {escape '\\'}");

备注

转义序列必须位于 SQL 语句的结尾。The escape sequence must be at the end of the SQL statement. 如果一个命令字符串中有多个 SQL 语句,则转义序列需要位于每个相关 SQL 语句的结尾。For multiple SQL statements in a command string, the escape sequence needs to be at the end of each relevant SQL statement.

函数处理Function handling

JDBC 驱动程序使用以下语法在 SQL 语句中支持函数转义序列:The JDBC driver supports function escape sequences in SQL statements with the following syntax:

{fn functionName}

其中,functionName 是由 JDBC 驱动程序支持的函数。where functionName is a function supported by the JDBC driver. 例如:For example:

SELECT {fn UCASE(Name)} FROM Employee

下表列出当使用函数转义序列时,JDBC 驱动程序支持的各种函数:The following table lists the various functions that are supported by the JDBC driver when using a function escape sequence:

字符串函数String Functions

数值函数Numeric Functions

日期时间函数Datetime Functions

系统函数System Functions

ASCIIASCII

CHARCHAR

CONCATCONCAT

DIFFERENCEDIFFERENCE

INSERTINSERT

LCASELCASE

LEFTLEFT

LENGTHLENGTH

LOCATELOCATE

LTRIMLTRIM

REPEATREPEAT

REPLACEREPLACE

RIGHTRIGHT

RTRIMRTRIM

SOUNDEXSOUNDEX

SPACESPACE

SUBSTRINGSUBSTRING

UCASEUCASE

ABSABS

ACOSACOS

ASINASIN

ATANATAN

ATAN2ATAN2

CEILINGCEILING

COSCOS

COTCOT

DEGREESDEGREES

EXPEXP

FLOORFLOOR

LOGLOG

LOG10LOG10

MODMOD

PIPI

POWERPOWER

RADIANSRADIANS

RANDRAND

ROUNDROUND

SIGNSIGN

SINSIN

SQRTSQRT

TANTAN

TRUNCATETRUNCATE

CURDATECURDATE

CURTIMECURTIME

DAYNAMEDAYNAME

DAYOFMONTHDAYOFMONTH

DAYOFWEEKDAYOFWEEK

DAYOFYEARDAYOFYEAR

EXTRACTEXTRACT

HOURHOUR

MINUTEMINUTE

MONTHMONTH

MONTHNAMEMONTHNAME

NOWNOW

QUARTERQUARTER

SECONDSECOND

TIMESTAMPADDTIMESTAMPADD

TIMESTAMPDIFFTIMESTAMPDIFF

WEEKWEEK

YEARYEAR

DATABASEDATABASE

IFNULLIFNULL

USERUSER

备注

如果您试图使用数据库不支持的函数,则将发生错误。If you try to use a function that the database does not support, an error will occur.

日期和时间文本Date and time literals

用于日期、时间和时间戳文本的转义语法如下所示:The escape syntax for date, time, and timestamp literals is the following:

{literal-type 'value'}

其中,literal-type 为以下值之一:where literal-type is one of the following:

文本类型Literal Type

说明Description

值格式Value Format

dd

DateDate

yyyy-mm-ddyyyy-mm-dd

tt

时间Time

hh:mm:ss [1]hh:mm:ss [1]

tsts

TimeStampTimeStamp

yyyy-mm-dd hh:mm:ss[.f...]yyyy-mm-dd hh:mm:ss[.f...]

例如:For example:

UPDATE Orders SET OpenDate={d '2005-01-31'}

WHERE OrderID=1025

存储过程调用Stored procedure calls

JDBC 驱动程序对于存储过程调用支持 {? = call proc_name(?,...)} 和 {call proc_name(?,...)} 转义语法,具体取决于是否需要处理返回参数。The JDBC driver supports the {? = call proc_name(?,...)} and {call proc_name(?,...)} escape syntax for stored procedure calls, depending on whether you need to process a return parameter.

过程是存储在数据库中的可执行对象。A procedure is an executable object stored in the database. 通常,它是一个或更多的已经预编译的 SQL 语句。Generally, it is one or more SQL statements that have been precompiled. 调用存储过程的转义序列语法如下所示:The escape sequence syntax for calling a stored procedure is the following:

{[?=]call procedure-name[([parameter][,[parameter]]...)]}

其中,procedure-name 指定存储过程的名称,parameter 指定存储过程参数。where procedure-name specifies the name of a stored procedure and parameter specifies a stored procedure parameter.

有关将 call 转义序列用于存储过程的详细信息,请参阅结合使用语句和存储过程。For more information about using the call escape sequence with stored procedures, see Using Statements with Stored Procedures.

外部联接Outer joins

JDBC 驱动程序支持 SQL92 左联接、右联接和完全外部联接语法。The JDBC driver supports the SQL92 left, right, and full outer join syntax. 外部联接的转义序列如下所示:The escape sequence for outer joins is the following:

{oj outer-join}

其中,外部联接为:where outer-join is:

table-reference {LEFT | RIGHT | FULL} OUTER JOIN

{table-reference | outer-join} ON search-condition

其中,table-reference 为表名,search-condition 为要用于这些表的联接条件。where table-reference is a table name and search-condition is the join condition you want to use for the tables.

例如:For example:

SELECT Customers.CustID, Customers.Name, Orders.OrderID, Orders.Status

FROM {oj Customers LEFT OUTER JOIN

Orders ON Customers.CustID=Orders.CustID}

WHERE Orders.Status='OPEN'

JDBC 驱动程序支持以下外部联接转义序列:The following outer join escape sequences are supported by the JDBC driver:

左外部联接Left outer joins

右外部联接Right outer joins

完全外部联接Full outer joins

嵌套外部联接Nested outer joins

限制转义语法Limit escape syntax

备注

在使用 JDBC 4.1 或更高版本时,LIMIT 转义语法仅受 Microsoft JDBC Driver 4.2 for SQL Server(或更高版本)支持。The LIMIT escape syntax is only supported by Microsoft JDBC Driver 4.2 (or higher) for SQL Server when using JDBC 4.1 or higher.

LIMIT 的转义语法如下所示:The escape syntax for LIMIT is as follows:

LIMIT [OFFSET ]

转义语法有两个部分: 是必需部分,用于指定要返回的行数 。The escape syntax has two parts: is mandatory and specifies the number of rows to return. OFFSET 和 都是可选部分,用于指定在开始返回行之前要跳过的行数 。OFFSET and are optional and specify the number of rows to skip before beginning to return rows. JDBC 驱动程序通过将查询转换为使用 TOP 而不是 LIMIT,仅支持必需部分。The JDBC driver supports only the mandatory part by transforming the query to use TOP instead of LIMIT. SQL Server 不支持 LIMIT 子句。SQL Server does not support the LIMIT clause. JDBC 驱动程序不支持可选的 ;如果使用它,驱动程序将引发异常 。The JDBC driver does not support the optional and the driver will throw an exception if it is used.

另请参阅See also

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java sql 查询中的转义序列不对_在 JDBC 中使用 SQL 转义序列 - SQL Server 的相关文章

  • NSSCTF web题记录

    目录 web GXYCTF 2019 BabyUpload htaccess利用 NISACTF 2022 babyserialize pop链 NISACTF 2022 popchains pop链 NSSRound 4 SWPU 1zw
  • Linux删除文件每一行的首尾空格等操作——sed简明教程

    cat tmp txt 123456789 123456789 123456789 删除每行第一个字符 sed s tmp txt 23456789 23456789 23456789 删除每行前两个字符 并保存到tmp2 txt sed
  • Java秒杀系统方案优化 2 --第2章 实现用户登录以及分布式session功能

    第2章 实现用户登录以及分布式session功能 1 明文密码两次md5入库 分别使用签名如1a2b3c4d 分别用签名和密码使用MD5加密两次后 一次是最原始密码加密 一次是加密后再使用MD5和签名加密 才存入数据库 每个用户对应都有一个
  • Dynamics 365 DevOps CI/CD之Solution

    CI CD到了Soution就没太多可说的了 按部就班配置就行 我选择的工具是Power DevOps Tool 1 首先下载工具 然后设置连接字符串去连环境 连接字符串还是用ClientSecret的形式 2 当然导出前还是要发布下自定义
  • Ue4蓝图访问外部接口

    首先我们搭建一个服务 node express 注意电脑必须安装node mkdir test server cd test server npm init npm install express body parser chalk pat
  • 数组复制的三种方法(超详细)

    数组复制的三种方法 Arrays类 copyOf 方法 copyOfRangs 方法 System类 arraycopy Arrays类 copyOf 方法 copyOf 方法适用于从下标0开始 复制指定长度的元素到目标数组 源码如下 pu
  • jQuery实现下拉菜单[代码+详细讲解+效果图]

    文章目录 前言 一 案例功能描述 二 html代码讲解 三 css代码讲解 四 jQuery实现功能及代码讲解 五 效果图 总结 前言 下拉菜单也是每个网页基本都会用到的案例 所以知识点还是要我们掌握的 这个案例比较简单 主要还是我们的jQ
  • 基于ftp协议的文件变化主动监听

    基于ftp协议的文件变化主动监听 前言 实现思路 代码实现思路 具体代码实现 依赖引入 FTPService接口 FTPServiceImpl类 FileChangeEvent接口 FileChangeData实体类 FileChangeT
  • linux 命令行与shell脚本编程大全

    linux 命令行与shell脚本编程大全 第一章 Linux LiveCD就是从cd读取的系统 由于没法将数据写入到cd 所以一旦重启 之前操作过后的一切数据都会丢失 第二章 第三章 1 man手册使用快捷键 使用空格键翻页 2 man
  • linux(centos) cpu核心数信息汇总

    总核数 物理CPU个数 X 每颗物理CPU的核数 总逻辑CPU数 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 查看物理CPU个数 cat proc cpuinfo grep physical id sort uniq wc l
  • 【论文速递】ECCV2022 - ByteTrack:通过关联每个检测盒来进行多对象跟踪

    论文速递 ECCV2022 ByteTrack 通过关联每个检测盒来进行多对象跟踪 论文原文 ByteTrack Multi Object Tracking by Associating Every Detection Box 论文地址 h
  • macos安装zsh

    https www cnblogs com xuLessReigns p 11005435 html mac下安装autojump brew install autojump 1 安装zsh 执行 sh c curl fsSL https
  • 端口扫描工具-Nmap

    Nmap是一款功能强大 功能丰富的网络扫描工具 对于网络管理员和安全专业人员来说 是一个非常有用的工具 并且被广泛应用于安全审计 网络调查 漏洞搜寻等领域 能扫描主机的端口 查看端口是否开放 端口的服务是什么 端口扫描不但可以为黑客所利用
  • 深入理解mysqldump原理 --single-transaction --lock-all-tables --master-data

    在mysqldump过程中 之前其实一直不是很理解为什么加了 single transaction就能保证innodb的数据是完全一致的 而myisam引擎无法保证 必须加 lock all tables 前段时间抽空详细地查看了整个mys
  • 宝塔linux ssh没发启用,宝塔终端面板连接SSH服务失败解决方案

    最近更新了宝塔面板的版本后 通过宝塔SSH终端 1 0工具链接时出现了SSH服务失败的问题 尝试使用云平台的SSH和XSHELL之类的工具都可以正常链接 可以排除是SSH端口或是设置错误造成的故障 通过翻看宝塔官方论坛 找到了大炮运维V58
  • PageHelper分页插件出现的异常:check the your MySQL server version for the right syntax to use near ‘LIMIT 5‘

    1 使用PageHelper分页插件时 编写测试方法 运行并测试时出现如下异常 Cause com mysql jdbc exceptions jdbc4 MySQLSyntaxErrorException You have an erro
  • [ERROR NumCPU]: the number of available CPUs 1 is less than the required 2

    出现上述错误的原因 因为物理机或者虚拟机不满足Kubernetes的基础配置造成的 而Kubernetes对GPU要求至少是2核 2G内存 W0123 08 22 23 322562 12102 validation go 28 Canno
  • 做编程题没有思路怎么办

    来信 老师您好 我是一名计算机专业大二的学生 我现在在做一系列c语言竞赛一些容易的题目 可是我发现我碰到的问题会很没有思路 不知道方向 看见网上的很多同学都能够解决 而我却不能 我不知道自己差到哪了 我不知道怎么办才好 都有很多中学生的水平
  • macOS 12 Monterey:一次全新的跨设备协作体验

    macOS 12 Monterey是苹果公司的一次重大突破 它打破了设备间的壁垒 将不同设备无缝地连接在一起 极大地提升了用户的工作效率和娱乐体验 Monterey带来了通用控制 AirPlay 捷径等新功能 以及一些实用的新小功能 安装

随机推荐