JDBC Statement vs PreparedStatement vs CallableStatement

JDBC Statement, PreparedStatement and CallableStatement are interfaces which provide ways to interact with Databases(Oracle / MySQL / PostGre…etc) with the help of methods in it. But these statements have some difference in terms of features they provide.

JDBC Statement

Used for firing static queries(e.g. select * from table). JDBC Statements can not accept parameters.

Syntax of Statement

		Statement statement = null;
		Connection connection = null;
		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {

			connection = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:" + dbName, userName,
					password);

		} catch (SQLException e) {

			e.printStackTrace();
		}

				statement = connection.createStatement();

				statement.execute(<Static Query>);

 

Prepared Statement

PreparedStatement extends Statement Interface to provide extra feature. It(PreparedStatement) is useful when firing the same SQL statement is required. PreparedStatement Interface accepts input parameter at runtime. PreparedStatement Objects are percompiled hence there execution is faster.

Syntax of PreparedStatement

 

		String inserRecordQuery= "insert into JBT values('beginne','Hibernate')";
		PreparedStatement  preparedStatement = null;
		Connection connection = null;

..................

				preparedStatement = connection.prepareStatement(inserRecordQuery);

				preparedStatement.executeUpdate();

 

CallableStatement

CallableStatement extends PreparedStatement Interface to provide more feature then PreparedStatement. It is used when database Stored Procedure needs to be invoked.

5 Comments JDBC Statement vs PreparedStatement vs CallableStatement

  1. Manish joshi

    not that much great but good content
    but it should be specify with some diagram and internal things then it will more helpfulL for beginner

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.