JDBC - What is java.sql.Statement in java


In this tutorial we will learn about java.sql.Statement in java JDBc.

10 points about java.sql.Statement in java jdbc?


  1. The java.sql.Statement object used for executing a static SQL statement and returning the results it produces in java.

  1. Statement cannot accept parameters at runtime in java.

  1. java.sql.Statement is slower as compared to PreparedStatement in java.

  1. java.sql.Statement is suitable for executing DDL commands - CREATE, drop, alter and truncate in java jdbc.
  2. java.sql.Statement can’t be used for storing/retrieving image and file in database (i.e. using BLOB, CLOB datatypes) in java.
  3. java.sql.Statement enforces SQL injection, because we end up using query formed using concatenated SQL strings in java jdbc.
Example in java >
String s1= "select * from EMPLOYEE where id = ";
int i1 = 2 ;
stmt = con.createStatement();
rs = stmt.executeQuery(s1 + String.valueOf(i1));
  1. same SQL query can’t be executed repeatedly in java.sql.Statement in java jdbc .
  2. java.sql.Statement  makes code less readable and understandable - We may need to write concatenated SQL strings in java.
  3. java.sql.Statement is an interface in java jdbc.

  1. By default, only one ResultSet object per Statement object is allowed to be opened at the same time in java.

  1. java.sql.Statement Important methods in java -





Programs where java.sql.Statement is used in java jdbc >


java.sql.Statement - using executeUpdate  and executeQuery methods CREATE, SELECT,INSERT, UPDATE, DELETE  in java>

JDBC- Statement example - Execute CREATE table query(DDL command) using Statement's executeUpdate method in java



JDBC- Statement example - Execute SELECT query(DML command) using Statement's executeQuery method and ResultSet in java






JDBC- Statement example - Execute INSERT query(DML command) using Statement's executeUpdate method in java





JDBC- Statement example - Execute UPDATE query(DML command) using Statement's executeUpdate method in java





JDBC- Statement example - Execute DELETE query(DML command) using Statement's executeUpdate method in java





Statement BATCH  - using executeUpdate method >

JDBC- Batch Statement example- Execute INSERT query(DML command) using Statement's executeUpdate, addBatch("sql"), executeBatch() methods in java






JDBC- Batch Statement example- Execute UPDATE query(DML command) using Statement's executeUpdate, addBatch("sql"), executeBatch() methods in java






JDBC- Batch Statement example- Execute DELETE query(DML command) using Statement's executeUpdate addBatch("sql"), executeBatch() methods in java


                   



Statement - using execute, getResultSet and getUpdateCount methods >

JDBC- Execute SELECT query using Statement's execute method, getResultSet method and ResultSet in java






JDBC- Execute INSERT query using Statement's execute and getUpdateCount method in java


JDBC- Execute UPDATE query using Statement's execute and getUpdateCount method in java





JDBC- Execute DELETE query using Statement's execute and getUpdateCount method in java



JDBC Transactions - commit and rollback (TCL command)

JDBC Transactions - commit and rollback(TCL command) - using Statement's executeUpdate method, setting connection.setAutoCommit(false) in java



Differences >

12 Differences between Statement, PreparedStatement and CallableStatement in java





In this tutorial we learned about java.sql.Statement in java JDBc.


RELATED LINKS>

java.sql.PreparedStatement - using executeUpdate  and executeQuery methods - CREATE, SELECT, INSERT, UPDATE and DELETE
Labels: Core Java JDBC
eEdit
Must read for you :