Monday, 29 April 2013

What are the difference between DDL, DML and DCL commands?



DDL

Data Definition Language (DDL) statements are used to define the DB structure or schema. i.e.:-
  • CREATE – providing a way to create objects in the db.
  • ALTER – capable to alter the structure of the db objects.
  • COMMENT – for addition of comments to the data dictionary.
  • TRUNCATE - remove all records from a table including all spaces allocated for the objects.
  • DROP – responsible for deleting objects from db.
  • RENAME – for renaming an object.
DML

Data Manipulation Language (DML) statements are used for managing data within schema objects. i.e.:-
  • INSERT – for the insertion of data into a table.
  • UPDATE – for updating existing data within a table.
  • DELETE – It deletes all records from a table but the space for the objects remain.
  • SELECT – for retrieving the data from the db.
  • LOCK TABLE - for controling concurrency.
  • CALL - calling a PL/SQL or Java subprogram.
  • EXPLAIN PLAN - explain access path to data.
  • MERGE - for performing insert or update operation.
DCL
  
Data Control Language (DCL)statements are used to manage user access into db i.e.-:
  • GRANT - gives user's access privileges to database.
  • REVOKE - withdraw access privileges given with the GRANT command.
  • COMMIT – It will allows saving the completed work.
  • ROLLBACK - restore db to original since the last COMMIT.
  • SAVEPOINT - identify a point in a transaction to which we can later roll back.
  • SET TRANSACTION – It helps to change transaction isolation level and rollback segment (which rollback segment needs to use).
Note: - COMMIT, SAVEPOINT and ROLLBACK are also known as TCL (Transaction Control Statements). These statements are used to manage the changes made by DML statements. It also allows statements to be grouped or combined together as a unit for performing logical transactions.  

Difference between execute, executeQuery, executeUpdate & executeBatch



Use of different execute() methods in JDBC:-

  • boolean execute(String SQL) :-  It returns a boolean value true if a ResultSet object can be retrieved; otherwise, it returns false. We can use this method to execute SQL DDL statements.
  • int executeUpdate(String SQL) : - It returns the no of rows updated in db during the execution of the SQL statement. We can use this method to execute SQL queries like, an INSERT, UPDATE, or DELETE Statement.
  • ResultSet executeQuery(String SQL) :- It returns a ResultSet object. We can use this method when we want to execute SELECT queries in db.
  • int[] executeBatch() :- JDBC allows a program to perform mutiple insertion or updation of data into database at single step. executeBatch() is responsible to execute batch on statement object.

Advantages of using JDBC batch update:

Using JDBC batch insert or update, we can improve the performance of the application because if we are using batch queries it means we are reducing round-trip towards databse. Thus we are reducing network calls which provides a better performance in application. While using batch insert or update, we should always prefer to use PreparedStatement because this is the best approach to get better performance. We should always prefer to set AutoCommit mode as false

Difference between Statement and PreparedStatement



Difference between Statement and  PreparedStatement

In Java, a statement object is responsible for sending the SQL statements to the database mean it is responsible to execute queries. In Statement, the SQL query will be compiled and executed every time. We can use statement for general purpose queries.
                                                              V/S
PreparedStatement is more powerful version of Java Statements and always quicker and easier than statement objects in order to handle SQL queries.We can use preparedStatement for parametric query. PreparedStatement gets pre compiled in database and access plan is also cached in database, which allows database to execute parametric query written using prepared statement much faster than normal query because it has less job to complete. In case of this statement, Db uses an already compiled and defined access plan, this allows prepared statement query to run faster than normal query. It is reducing the load in the db. PreparedStatement helps to prevent SQL injection attacks in java because all the parameters passed as part of place holder will be escaped automatically by JDBC driver. PreparedStatement doesn't allow multiple values for one placeholder (?)  So it is difficult to use IN clause with SQL query.
Below example is a demo through which we can prevent the SQL injection. So we can say that prepare statement is providing more security in application.

PreparedStatement pstmt = con.prepareStatement("select firstname from student where student_id=?");

pstmt.setInt(1,studId);

Note:- '?' also indicates bind variable which is binded with the query.
The work process of PreparedStatement is as follows:
  • Prepare: The statement template is created by the application and sent to the database management system(DBMS). Placeholders/Bind variables or parameters are left unspecified

"insert into student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) values(?,?,?,?,?,?)";
  • The DBMS parses, compiles, and performs query optimization on the statement template, and stores the result without executing it.Execute: At later, when the application supplies placeholder’s values for the parameters, and the DBMS executes the statement (possibly returning a result). The application may execute the statement as many times as it wants with different values. In above example, we can supply any values for the placeholders. 
  • So, PreparedStatement reduce the overhead of compiling and optimizing the statement is happened only once as the SQL query can be executed multiple times. Prepared statements are widely supported by major DBMSs, including Microsoft SQL Server, DB2, MySQL, PostgreSQL and Oracle.

Relational Databases handles JDBC/SQL queries by following four steps:-
  • Parsing the incoming SQL queries.
  • Compiling the SQL queries.
  • Planning and optimizing the data acquisition path.
  • Executing the optimized query or obtaining the returned data.

A Statement will always processed through the above four steps for each SQL query sent to the database.
On the other hand, a PreparedStatement pre-executes steps 1 to 3 in the execution process. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. Only first time, PreparedStatement will perform all the four steps for handling SQL queries. So, the result is to reduce the load on the database engine during execution time.

JDBC - Rowset




RowSet: - It is an object that combines a set of rows from either JDBC result sets or tabular data sources.  It supports component-based development models such as JavaBeans, with a standard set of properties and an event notification mechanism.

Types of RowSets:-

  • CachedRowSet
  • JdbcRowSet
  • WebRowSet
  • FilteredRowSet
  • JoinRowSet
Implementation classes are:-
  •  com.sun.rowset.CachedRowSetImpl
  • com.sun.rowset.JdbcRowSetImpl
  • com.sun.rowset.WebRowSetImpl
  • com.sun.rowset.FilteredRowSetImpl
  • com.sun.rowset.JoinRowSetImpl
RowSets are divided into two categories:-
  • Connected Rowset :- The connected rowset is connected to the db connection object like resultset. It is making the permanent connection and doesn’t terminate until the application is terminated.Example :-  JdbcRowSet.
  • Disconnected RowSet: - A disconnected RowSet retrieves the data from DB whenever requires, it establishes the connection and closes it after finishing the required task. The data that is modified during disconnected state is updated after the connection is re-established. Example:- CachedRowSet, WebRowSet, FilteredRowSet, JoinRowSet.
RowSet Sample Code:-


package com.gaurav.jdbcexamples;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;
import javax.sql.rowset.JdbcRowSet;

import com.sun.rowset.JdbcRowSetImpl;

public class JDBCRowSetDemo {

      privatestatic JdbcRowSet jdbcRowSet = null;
      privatestatic Connection con = null;

      publicstatic Connection getMySqlConnection() throws Exception {

            String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // MySQL Driver filename
            String USERNAME = "root";
            String PASSWORD = "root";
            String DATABASE_URL = "jdbc:mysql://localhost:3306/test";

            Class.forName(JDBC_DRIVER);
            Connection connection = DriverManager.getConnection(DATABASE_URL,
                        USERNAME, PASSWORD);
            return connection;
      }

      publicstatic void main(String... args) {
            try {

                  con = getMySqlConnection();
                  System.out.println(" SUCCESSFULLY GOT THE CONNECTION ");
                  jdbcRowSet = new JdbcRowSetImpl(con);
                  jdbcRowSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);
                  String query = "SELECT * FROM student";
                  jdbcRowSet.setCommand(query);
                  jdbcRowSet.execute();
                  jdbcRowSet.addRowSetListener(new TestListener());

                  System.out
                              .println("------------------- ROWSET IS MOVING FORWARD ------------------------------");

                  while (jdbcRowSet.next()) {
                        // cursor Movement event occurs here
                        System.out.println("STUDENT_ID - " + jdbcRowSet.getInt(1));
                        System.out.println("FIRSTNAME - " + jdbcRowSet.getString(2));
                  }

                  System.out
                              .println("------------------- ROWSET IS MOVING BACK ------------------------------");

                  while (jdbcRowSet.previous()) {
                        // cursor Movement event occurs here
                        System.out.println("STUDENT_ID - " + jdbcRowSet.getInt(1));
                        System.out.println("FIRSTNAME - " + jdbcRowSet.getString(2));
                  }

                  System.out
                              .println("------------------ ROWSET IS MOVING RANDOMLY -----------------------------");

                  boolean isAbsoulteRow = jdbcRowSet.absolute(2);
                  System.out.println("isAbsoulteRow->" + isAbsoulteRow);

            } catch (SQLException sqe) {
                  sqe.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  try {
                        if (jdbcRowSet != null)
                              jdbcRowSet.close();
                        if (con != null)
                              con.close();
                  } catch (Exception e) {
                        e.getMessage();
                  }
            }
      }
}

class TestListener implements RowSetListener {

      @Override
      publicvoid cursorMoved(RowSetEvent event) {
            System.out.println("EVENT OCCURRED FOR CURSOR MOVEMENT");
      }

      @Override
      publicvoid rowChanged(RowSetEvent event) {
            System.out.println("EVENT OCCURRED FOR CURSOR CHANGED");
      }

      @Override
      publicvoid rowSetChanged(RowSetEvent event) {
            System.out.println("EVENT OCCURRED FOR ROWSET CHANGED");
      }
}


Result:-