Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who implements Connection, ResultSet and Statement interfaces in this java program? [duplicate]

Tags:

java

interface

I recently found out that the Statement, Connection and the ResultSet used in the following program are interfaces. This program works completely fine but who implements these interfaces ?

package jdbc;

import java.sql.*;

public class Mango {

    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");   
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@66.66.66.128:1521:xe","SYSTEM","matrix");
        Statement comm = con.createStatement();
        comm.executeQuery("insert into a values('a',1)");
        ResultSet res = comm.executeQuery("SELECT * FROM A");
        comm.executeQuery("insert into a values('a',1)");
        while(res.next()) {
            System.out.println(res.getString(1) + " " + res.getInt(2));
        }
    }

}
like image 869
Cyclops Avatar asked Nov 30 '25 19:11

Cyclops


2 Answers

Interfaces Statement, Connection and the ResultSet are finally implemented by the third party JDBC Driver provider.

Suppose if you are using MySQL Driver, this is how the hierarchy of implementation follows,

  1. java.sql.Statement interface is finally implemented by com.mysql.jdbc.StatementImpl Class.

Internal implementation hierarchy:

com.mysql.jdbc.StatementImpl(Class) -->implements--> com.mysql.jdbc.Statement(interface) -->extends--> java.sql.Statement(interface)

  1. java.sql.Connection interface is finally implemented by com.mysql.jdbc.JDBC4Connection Class.

Internal implementation hierarchy:

com.mysql.jdbc.JDBC4Connection(Class) -->extends--> com.mysql.jdbc.ConnectionImp(Class) -->extends--> com.mysql.jdbc.ConnectionPropertiesImpl(Class) -->implements--> com.mysql.jdbc.MySQLConnection(Interface) -->extends--> com.mysql.jdbc.Connection(Interface) -->extends--> java.sql.Connection(Interface)

  1. java.sql.ResultSet interface is finally implemented by com.mysql.jdbc.ResultSetImpl Class.

Internal implementation hierarchy:

com.mysql.jdbc.ResultSetImpl(Class) -->implements--> com.mysql.jdbc.ResultSetInternalMethods(Interface) -->extends--> java.sql.ResultSet(Interface)

like image 98
Sandeep Patange Avatar answered Dec 03 '25 12:12

Sandeep Patange


The JDBC API is implemented through the JDBC driver which are being provided by the various Database software vendors.

The JDBC Driver is a set of classes that implement the JDBC interfaces to process JDBC calls and return result sets to a Java application

like image 31
Kartik73 Avatar answered Dec 03 '25 11:12

Kartik73



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!