All Products
Search
Document Center

PolarDB:Using the Prepare protocol

Last Updated:Mar 30, 2026

PolarDB-X 1.0 (Distributed Relational Database Service) supports the client/server binary protocol for executing server-side prepared statements. This page explains how the protocol works, which SQL statements it supports, and how to configure a Java client to use it.

Benefits

Prepared statements separate SQL structure from parameter values. The database receives the statement template once, parses and optimizes it, then accepts only parameter values for each subsequent execution. This separation provides two concrete advantages:

  • Reduced parsing overhead: The database parses and optimizes the statement once and reuses the execution plan for every subsequent run. When your application executes many near-identical statements that differ only in parameter values, this eliminates repeated parsing costs across the entire statement.

  • SQL injection protection: Because parameters are transmitted separately from the SQL structure, user-supplied input is never interpreted as SQL syntax — the database always treats it as a value, not as a command.

Supported commands

The protocol supports the following MySQL binary protocol commands:

Command Description
COM_STMT_PREPARE Sends a statement template to the server for parsing and optimization
COM_STMT_EXECUTE Executes a previously prepared statement with bound parameter values
COM_STMT_CLOSE Releases the server-side resources held by a prepared statement
COM_STMT_RESET Resets a prepared statement to its initial state without closing it

The protocol works with Java and other programming languages. For a full reference on prepared statement commands in MySQL, see Prepared statements.

SQL statement support

All DML statements can be used as prepared statements, including SELECT, UPDATE, DELETE, and INSERT.

Non-DML statements cannot be used as prepared statements. SHOW and SET are not supported.

The following statements cannot be used as prepared statements in a MySQL CLI:
mysql> SET @s = 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> PREPARE stmt2 FROM @s;
mysql> SET @a = 6;
mysql> SET @b = 8;
mysql> EXECUTE stmt2 USING @a, @b;

Enable server-side prepared statements in a Java client

By default, the MySQL JDBC driver uses client-side prepared statements — the driver handles statement preparation locally without sending COM_STMT_PREPARE to the server. To use server-side prepared statements, add useServerPrepStmts=true to your JDBC connection URL. Without this parameter, the driver sends a regular query even when you call prepareStatement() in your code.

Example connection URL:

jdbc:mysql://xxxxxx:3306/xxxxxx?useServerPrepStmts=true

Java example

The following example connects to PolarDB-X 1.0 and runs an INSERT using server-side prepared statements:

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(
    "jdbc:mysql://xxxxxx:3306/xxxxxx?useServerPrepStmts=true",
    "xxxxx",
    "xxxxx"
);
String sql = "insert into batch values(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 0);
preparedStatement.setString(2, "corona-db");
preparedStatement.executeUpdate();

Replace the placeholder values in the connection URL and credentials with your actual endpoint, database name, username, and password.