对于 Connector/J 8.0.26 及更高版本: 
        Connector/J组件
        在服务器上启用
        后支持查询属性(有关详细信息,请参阅使用查询属性的先决条件)。
      query_attributes
setAttribute()使用接口的方法为
        
        查询设置属性
        JdbcStatement。这是方法的签名:
      
JdbcStatement.setAttribute(String name, Object value)
        以下是将查询属性与 a 一起使用的示例
        JdbcStatement:
      
示例 6.1 将查询属性与普通语句一起使用
conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "myuser", "password");
Statement stmt = conn.createStatement();
JdbcStatement jdbcStmt = (JdbcStatement) stmt;
jdbcStmt.executeUpdate("CREATE TABLE t11 (c1 CHAR(20), c2 CHAR(20))");
jdbcStmt.setAttribute("attr1", "cat");
jdbcStmt.setAttribute("attr2", "mat");
jdbcStmt.executeUpdate("INSERT INTO t11 (c1, c2) VALUES(\n" +
    " mysql_query_attribute_string('attr1'),\n" +
    " mysql_query_attribute_string('attr2')\n" +
    " );");
ResultSet rs = stmt.executeQuery("SELECT * from t11");
while(rs.next()) {
    String col1 = rs.getString(1);
    String col2 = rs.getString(2);
    System.out.println("The "+col1+" is on the "+col2);
    }
        虽然查询属性在每次查询后在服务器上被清除,但它们保留在 Connector/J 端,因此可以为下一个查询重新发送它们。清除属性,使用接口的
        clearAttributes()方法
        JdbcStatement:
      
JdbcStatement.clearAttributes()以下示例( 示例 6.1“使用带有普通语句的查询属性”中代码的延续)显示了如何为语句保留属性直到它被清除:
示例 6.2 查询属性的保存
/* Continuing from the code in the last example, where query attributes have 
already been set and used */ 
rs = stmt.executeQuery("SELECT c2 FROM t11 where " +
                "c1 = mysql_query_attribute_string('attr1')");
        if (rs.next()) {
            String col1 = rs.getString(1);
            System.out.println("It is on the "+col1);
        }
        // Prints "It is on the mat"
        jdbcStmt.clearAttributes();
        rs = stmt.executeQuery("SELECT c2 FROM t11 where " +
                "c1 = mysql_query_attribute_string('attr1')");
        if (rs.next()) {
            String col1 = rs.getString(1);
            System.out.println("It is on the "+col1);
        }
        else {
            System.out.println("No results!");
        }
        // Prints "No results!" as attribute string attr1 is empty
        还可以使用以下setAttribute()
        方法为客户端和服务器端准备好的语句设置属性:
      
示例 6.3 将查询属性与准备好的语句一起使用
conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "myuser", "password");
PreparedStatement ps = conn.prepareStatement(
                "select ?, c2 from t11 where c1 = mysql_query_attribute_string('attr1')");
ps.setString(1, "It is on a ");
JdbcStatement jdbcPs = (JdbcStatement) ps;
jdbcPs.setAttribute("attr1", "cat");
rs = ps.executeQuery();
if (rs.next()) {
            System.out.println(rs.getString(1)+" "+ rs.getString(2));
}
        该方法不支持所有 MySQL 数据类型
        setAttribute();仅支持以下 MySQL 数据类型,并直接映射到特定 Java 对象或其子类:
      
表 6.22 查询属性的数据类型映射
| MySQL 数据类型 | 对象 | 
|---|---|
|  MYSQL_TYPE_STRING | java.lang.String | 
|  MYSQL_TYPE_TINY | java.lang.Boolean,java.lang.Byte | 
| MYSQL_TYPE_SHORT | java.lang.Short | 
| MYSQL_TYPE_LONG | java.lang.Integer | 
| MYSQL_TYPE_LONGLONG | java.lang.Long,java.math.BigInteger | 
| MYSQL_TYPE_FLOAT | java.lang.Float | 
| MYSQL_TYPE_DOUBLE | java.lang.Double,java.math.BigDecimal | 
| MYSQL_TYPE_DATE | java.sql.Date,java.time.LocalDate | 
| MYSQL_TYPE_TIME | java.sql.Time,java.time.LocalTime,java.time.OffsetTime,java.time.Duration | 
| MYSQL_TYPE_DATETIME | java.time.LocalDateTime | 
| MYSQL_TYPE_TIMESTAMP | j ava.sql.Timestamp,java.time.Instant,java.time.OffsetDateTime,java.time.ZonedDateTime,java.util.Date_java.util.Calendar | 
        当没有从 Java 对象类型到任何 MySQL 数据类型的直接映射时,该属性设置为一个字符串值,该字符串值来自将提供的对象转换为
        String使用该
        .toString()方法的对象。