一、声明:
它用于访问您的数据库。 Statement 接口不能接受参数,当您在运行时使用静态 SQL 语句时很有用。如果您只想运行 SQL 查询一次,则此接口优于 PreparedStatement。
例子 –
//Creating The Statement Object
Statement GFG = con.createStatement();
//Executing The Statement
GFG.executeUpdate("CREATE TABLE STUDENT(ID NUMBER NOT NULL, NAME VARCHAR)");
2. 可调用语句:
当您要使用数据库存储过程时使用它。 CallableStatement 可以接受运行时输入参数。
例子 –
//Creating CallableStatement object
CallableStatement GFG = con.prepareCall("{call anyProcedure(?, ?, ?)}");
//Use GFG.setter() methods to pass IN parameters
//Use GFG.registerOutParameter() method to register OUT parameters
//Executing the CallableStatement
GFG.execute();
//Use GFG.getter() methods to retrieve the result
Statement 和 CallableStatement 的区别:
Statement | CallableStatement |
---|---|
It is used when SQL query is to be executed only once. | It is used when the stored procedures are to be executed. |
You can’t pass the parameters at runtime. | You can pass the parameters at runtime. |
Used for CREATE, ALTER, DROP statements. | Used to execute functions. |
Performance is very low. | Performance is better than Statement. |
Used to execute normal SQL queries. | Used to call the stored procedures. |
It is base interface. | It extends PreparedStatement interface. |
It is used for DDL statements. | It is used for stored procedures. |
We can not used statement for reading binary data.. | We can used CallableStatement for reading binary data.. |