Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

JDBC SQL Escape Syntax คืออะไร? อธิบาย


Escape syntax ให้ความยืดหยุ่นใช้ฟีเจอร์เฉพาะฐานข้อมูลที่ JDBC มาตรฐานไม่รองรับ รูปแบบทั่วไป:

{d 'yyyy-mm-dd'}

yyyy=ปี mm=เดือน dd=วัน ใช้ {d '2009-09-03'} คือ 3 กันยายน 2009

ตัวอย่าง

// สร้าง Statement
stmt = conn.createStatement();
// Insert ข้อมูล: ID, First Name, Last Name, DOB
String sql = "INSERT INTO STUDENTS VALUES" + "(100,'Zara','Ali', {d '2001-12-16'})";
stmt.executeUpdate(sql);

escape Keyword

ระบุ escape character ใน LIKE clause เหมาะกับ wildcard % เช่น:

String sql = "SELECT symbol FROM MathSymbols WHERE symbol LIKE '\%' {escape '\'}";
stmt.execute(sql);

ถ้าใช้ backslash (\) ต้องเขียน \\ ใน Java string เพราะ \ เป็น escape char ของ Java ด้วย

fn Keyword

แทน scalar functions ของ DBMS เช่น length():

{fn length('Hello World')}

คืนค่า 11 (ความยาวสตริง)

call Keyword

เรียก stored procedures:

{call my_procedure(?)};

ถ้ามี IN parameter และ OUT parameter:

{? = call my_procedure(?)};

oj Keyword

Outer joins syntax:

{oj outer-join}

outer-join = table {LEFT|RIGHT|FULL} OUTER JOIN {table | outer-join} on search-condition

String sql = "SELECT Employees FROM {oj ThisTable RIGHT OUTER JOIN ThatTable on id = '100'}";
stmt.execute(sql);