3

According to javadoc, createStatement() method creates a Statement instance for sending SQL statements to the database.

Now Statement is an interface under java.sql package and my understanding is that it is not possible to create an instance of an interface in Java.

Then how is it working? In source I found this, only I don't understand.

/**
 * Creates a <code>Statement</code> object for sending
 * SQL statements to the database.
 * SQL statements without parameters are normally
 * executed using <code>Statement</code> objects. If the same SQL statement
 * is executed many times, it may be more efficient to use a
 * <code>PreparedStatement</code> object.
 * <P>
 * Result sets created using the returned <code>Statement</code>
 * object will by default be type <code>TYPE_FORWARD_ONLY</code>
 * and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
 * The holdability of the created result sets can be determined by
 * calling {@link #getHoldability}.
 *
 * @return a new default <code>Statement</code> object
 * @exception SQLException if a database access error occurs
 * or this method is called on a closed connection
 */
Statement createStatement() throws SQLException;
Robin Green
  • 32,079
  • 16
  • 104
  • 187
Dhruv Kapatel
  • 873
  • 3
  • 14
  • 27
  • conn.createStatement() DOES NOT mean there is a code like 'new Statement()' exists in the method. – LHA Dec 13 '13 at 21:00
  • 4
    The createStatement returns a reference to an object that implements the Statement interface. – DwB Dec 13 '13 at 21:04
  • 2
    Related: http://stackoverflow.com/questions/7550612/in-simplest-terms-what-is-a-factory/ – BalusC Dec 13 '13 at 21:07
  • @DwB can you describe your answer or explain internal working behind this? – Dhruv Kapatel Dec 13 '13 at 21:07
  • 1
    @Dhruv: have you really never wondered how e.g. `List list = new ArrayList();` works given that `List` is an interface? – BalusC Dec 13 '13 at 21:09
  • @Dhruv you have misunderstood. You can create an instance of a class that inherits an interface, and that is an instance of that interface - as `instanceof` will prove. – Robin Green Dec 13 '13 at 22:36

3 Answers3

4

It needs a JDBC driver in order to work. The JDBC driver implements the interfaces described in java.sql. The driver's implementation of Connection has an implementation of the createStatement method that returns the driver's implementation of Statement.

JDBC is about letting vendors provide their own vendor-specific implementations of JDBC while giving users a uniform interface. A database provides a JDBC driver, the driver implements the interfaces: If you look inside the jar file for a JDBC Driver you'll see a lot of vendor-specific classes like:

WhateverRdbmsConnectionImpl
WhateverRdbmsPreparedStatementImpl
...

etc., where each implements a java.sql interface.

So you might see

public class WhateverRdbmsConnectionImpl implements java.sql.Connection {

    public java.sql.Statement createStatement() throws java.sql.SQLException {
        WhateverRdbmsStatementImpl stmt = new WhateverRdbmsStatementImpl();
        ...
        return stmt;
    }
}

createStatement is required to return something that implements the interface java.sql.Statement, vendors provide their own implementation.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
2

Here is some example code:

interface IBlammy
{
    public String blam();
}

class BlammyImpl
implements IBlammy
{
    public String blam()
    {
        return "kapow";
    }
}


class Hooty
{
    public IBlammy getStatement()
    {
        return new BlammyImpl();
    }
}

Hooty.getStatement() is returning a reference to an object that implements the IBlammy interface.

DwB
  • 37,124
  • 11
  • 56
  • 82
2

You're looking at the source for the interface of Connection, not a class. Interfaces don't contain any sort of implementation - they just define a contract, and classes that implement that interface are bound to implement everything on that interface.

http://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html

matt
  • 9,113
  • 3
  • 44
  • 46