文件首頁
MySQL Connector/J 開發人員指南
相關文件 下載本手冊
PDF (美式信紙) - 1.2Mb
PDF (A4) - 1.2Mb


6.8 使用查詢屬性

當伺服器上透過安裝 query_attributes 元件啟用時,Connector/J 支援查詢屬性(詳細資訊請參閱 使用查詢屬性的先決條件)。

透過使用 JdbcStatement 介面的 setAttribute() 方法,為查詢設定屬性。以下是方法簽章

JdbcStatement.setAttribute(String name, Object value)

以下是在 JdbcStatement 中使用查詢屬性的範例

範例 6.1 在純語句中使用查詢屬性

conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/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 端,因此可以在下一次查詢時重新傳送。若要清除屬性,請使用 JdbcStatement 介面的 clearAttributes() 方法

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://127.0.0.1/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));
}

setAttribute() 方法並非支援所有 MySQL 資料類型;僅支援以下 MySQL 資料類型,並直接從特定的 Java 物件或其子類別對應而來

表 6.22 查詢屬性的資料類型對應

MySQL 資料類型 Java 物件
MYSQL_TYPE_STRING java.lang.String
MYSQL_TYPE_TINY java.lang.Booleanjava.lang.Byte
MYSQL_TYPE_SHORT java.lang.Short
MYSQL_TYPE_LONG java.lang.Integer
MYSQL_TYPE_LONGLONG java.lang.Longjava.math.BigInteger
MYSQL_TYPE_FLOAT java.lang.Float
MYSQL_TYPE_DOUBLE java.lang.Doublejava.math.BigDecimal
MYSQL_TYPE_DATE java.sql.Datejava.time.LocalDate
MYSQL_TYPE_TIME java.sql.Timejava.time.LocalTimejava.time.OffsetTimejava.time.Duration
MYSQL_TYPE_DATETIME java.time.LocalDateTime
MYSQL_TYPE_TIMESTAMP java.sql.Timestampjava.time.Instantjava.time.OffsetDateTimejava.time.ZonedDateTimejava.util.Datejava.util.Calendar

當 Java 物件類型沒有直接對應到任何 MySQL 資料類型時,會使用將提供的物件轉換為 String (透過使用 .toString() 方法) 而來的字串值設定屬性。