JDBC- JDBC API

JDBC API

DriverManager class

  • The DriverManager class acts as an interface between user and drivers

  • It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver.

  • The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver().

Methods

  1. public static void registerDriver(Driver driver):

  2. public static void deregisterDriver(Driver driver):

  3. public static Connection getConnection(String url):

  4. public static Connection getConnection(String url,String uname,String pwd)

Connection interface

A Connection is the session between java application and database. The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e.

  • Statement createStatement()

  • PreparedStatement prepareStatement(String sql)

  • CallableStatement prepareCall(String sql)

  • Blob createBlob()

  • Clob createClob()

  • String getSchema()

  • DatabaseMetaData getMetaData()

  • void close()

  • void commit()

  • void rollback()

  • void setAutoCommit(boolean autoCommit)

  • boolean getAutoCommit()

All above statement related methods can have

  • ResultSet.TYPE_SCROLL_SENSITIVE used to move ResultSet Both Directions

  • ResultSet.CONCUR_UPDATABLE used to perform DML operations on ResultSet

Statement interface

  • The Statement interface provides methods to execute queries with the database.

  • The statement interface is a factory of ResultSet

  • it provides factory method to get the object of ResultSet

  • public ResultSet executeQuery(String sql):

    Is used to execute SELECT query. It returns the object of ResultSet.

  • public int executeUpdate(String sql):

    Is used to execute DML, CREATE, DROP, INSERT, UPDATE, DELETE etc.

  • public boolean execute(String sql):

    Is used to execute queries that may return multiple results. >1 if success, 0 on fail

  • public int[] executeBatch():

    Is used to execute batch of commands.

ResultSet interface

  • The object of ResultSet maintains a cursor pointing to a row of a table.

  • Initially, cursor points to before the first row.

  • By default, ResultSet object can be moved forward only and it is not updatable.

    • public XXX getXXX(int rowIndex):

    • public XXX getXXX(String rowName):

    • public boolean next()

    • public boolean previous()

    • public boolean first()

    • public boolean last()