---
title: JDBC 参数 useServerPrepStmts 使用介绍-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于JDBC 参数 useServerPrepStmts 使用介绍相关的常见问题和使用技巧，帮助您快速解决JDBC 参数 useServerPrepStmts 使用介绍的难题。
image: https://mdn.alipayobjects.com/huamei_22khvb/afts/img/A*OSPzQ6GUQF4AAAAAQHAAAAgAeiGDAQ/original
---
[OceanBase AI 数据大会](https://www.oceanbase.com/conference2026 "OceanBase AI 数据大会")

## 体验新一代 AI 数据平台

[立即咨询](https://www.oceanbase.com/contactus) [免费试用 免费试用](https://www.oceanbase.com/free-trial)划线反馈

# JDBC 参数 useServerPrepStmts 使用介绍

更新时间：2026-07-17 07:16

内容类型：TechNote  

本文介绍 JDBC 参数 useServerPrepStmts 的含义与使用方法。

## 适用版本

OceanBase 数据库 V3.x 版本。

## 详细内容

MySQL Protocol 有一个很重要的概念就是文本协议和二进制协议，简单的来说可以总结为：

- 文本协议：将 JDBC 的所有动作转换为标准的 SQL 语句，将 SQL 语句以字符串的形式发送到 OBServer 节点，OBServer 节点执行后再返回相应的结果。
 - 二进制协议：通过协议中的 COM_STMT_PREPARE 命令来 Prepare 相应的 SQL 语句，OBServer 节点解析 SQL 语句，返回相应参数的描述信息，驱动方面填充相应的数据信息，再发送到 OBServer 节点。

文本协议的好处是不用执行一次 Prepare，而是只执行相应的 SQL；二进制协议的好处是可以 Prepare 后多次执行。与之紧密相关的配置是 useServerPrepStmts，当它为 False 的时候采用文本协议，为 True 的时候会采用二进制协议。

如下的测试语句，在 useServerPrepStmts=true 和 useServerPrepStmts=false 时表现是不同的：

```shell
PreparedStatement ps = conn.prepareStatement("select * from selecttest");
ResultSet rs = ps.executeQuery();

```

- useServerPrepStmts=true：SQL 执行的过程为先做 prepare，然后再 execute。

  ```shell
  Request Prepare Statement
  Response
  Request Execute Statement
  Response

  ```
 - useServerPrepStmts=false 时，不会单独进行 prepare，直接 execute SQL 语句。

  ```shell
  Request Query {select * from selecttest}
  Response

  ```

PrepareStatement JDBC 是最常用的接口之一，表示预编译的 SQL 语句的对象，SQL 语句被预编译并存储在对象中，被封装的 SQL 语句代表某一类操作，SQL 语句中允许包含动态参数"？"，在执行时可以为"？"动态设置参数值。要实现真正的预编译效果需要设置 url option中的useServerPrepStmts = true，否则即使使用 PrepareStatement 也会降级成文本协议而不是二进制协议。

以下为示例代码：

```shell
package sqldemo;

import java.sql.*;

public class Main {

    public static void main(String[] args) throws SQLException{
        //URL指向要访问的数据库
        String url = "jdbc:oceanbase://11.xxx.xxx.xxx:2881/SYS?&useServerPrepStmts=true&cachePrepStmts=true&prepStmtCacheSize=10000";
        //MySQL配置时的用户名
        String user = "SYS@xxxxxx";
        //MySQL配置时的密码
        String password = "xxxx@@xxx";
        Connection conn = null;
        try {
            Class.forName("com.alipay.oceanbase.jdbc.Driver");                                                                       //驱动类名
            conn = DriverManager.getConnection(url, user, password);
            PreparedStatement ps = conn.prepareStatement("select  c1,(case when c2 !=0 then ? else c2 end) c2 from text1\n");
            int trans_date = 123333333;
            ps.setInt(1,trans_date);
            ResultSet rs = ps.executeQuery();
            rs.next();
            System.out.println("date is:" + rs.getString(1) +";"+ rs.getString(2));
            rs.close();
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (null != conn) {
                conn.close();
            }
        }
    }
}

```

Previous

[调用存过后 JDBC4Connection 对象泄漏](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000500229)

Next

[Java 客户端 net_write_timeout 值报错](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000106974) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ) 咨询
