GeekMantra

JDBC Standard APIs

1. Registering a Driver
    -statically load driver
         Class.forName(“foo.bar.MyDriver”);
         Connection c = DriverManager.getConnection(...);

    -or use the jdbc.drivers system property

2. JDBC URLs
    -jdbc:subprotocol:source
    each driver has its own subprotocol and each subprotocol has its own syntax

JDBC URL

3. DriverManager
    Connects to given JDBC URL with given user name and password
    Throws java.sql.SQLException
    returns a Connection object

JDBC Connection API

Connection
1. A Connection represents a session with a specific database.
2. Within the context of a Connection, SQL statements are executed and results are returned.
3. Can have multiple connections to a database
    NB: Some drivers don’t support serialized connections
    Fortunately, most do (now)
4. Also provides “metadata” -- information about the database, tables, and fields
5. Also methods to deal with transactions

Obtaining a Connection
String url   = "jdbc:odbc:mydsn";
try {
    Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection(url);
}
catch (ClassNotFoundException e)
    { e.printStackTrace(); }
catch (SQLException e)
    { e.printStackTrace(); }

Connection Methods
  -Statement createStatement()
    returns a new Statement object
  -PreparedStatement prepareStatement(String sql)
   returns a new PreparedStatement object
  -CallableStatement prepareCall(String sql)
    returns a new CallableStatement object

JDBC Statement APIs

1. Statement
A Statement object is used for executing a static SQL statement and obtaining the results produced by it.

Statement Methods

ResultSet executeQuery(String)
Execute a SQL statement that returns a single ResultSet.
int executeUpdate(String)
Execute a SQL INSERT, UPDATE or DELETE statement. Returns the number of rows changed.
boolean execute(String)
Execute a SQL statement that may return multiple results.

2. The PreparedStatement Interface

• The prepareStatement() method of Connection interface returns a PreparedStatement object
• Useful for frequently executed SQL statements
• An SQL statement is pre-compiled and gets executed more efficiently than a plain statement

   For example: the statement “INSERT INTO Table1 VALUES (?, ?)” can be used to add multiple records, with a different values each time. Where ‘?’ acts as a placeholder

JDBC Prepared Statement

3. The CallableStatement Interface

• A CallableStatement object is created by calling the prepareCall() method on a Connection object
• The object provides a way to call stored procedures in a standard way for all DBMS
• CallableStatement inherits Statement methods, which deal with SQL statements in general, and it also inherits PreparedStatement methods, which deal with IN parameters
• All methods defined in the CallableStatement deal with OUT parameters or the output aspect of INOUT parameters
• Syntax for a stored procedure without parameters is:
   {call procedure_name}
• Syntax for procedure call with two parameters:
   {call procedure_name[(?, ?)] }