JDBC- Implementation of interfaces

后端 未结 5 1077
青春惊慌失措
青春惊慌失措 2020-11-29 13:53

In JDBC, to connect and execute statements in DB we mainly make use of Connection,Statement and ResultSet which are interfaces. But their corresponding objects is later use

相关标签:
5条回答
  • 2020-11-29 14:32

    Hope the following explanation will help.

    Q: Where does the driver Connection implementation come from? And the implementation of Statement and ResultSet?

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    // String url="jdbc:mysql://localhost:3306/mydb?user=root&password=root";
    Connection con = DriverManager.getConnection(url);
    

    Small tip: the driver implementation is loaded as soon as the DriverManager class is loaded(initialized).

    Take the source code of mysql-connector-java:5.1.36 for example.

    ● class com.mysql.jdbc.Driver extends class NonRegisteringDriver, in which there is a method named connect() which returns an instance of Connection.
    pay attention to the registeredDrivers field of NonRegisteringDriver

    public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    ...
    public java.sql.Connection connect(String url, Properties info) throws SQLException {
    

    ● mysql driver is registered and the driver is added to registeredDrivers:

    Class.forName("com.mysql.jdbc.Driver");
    ...
    java.sql.DriverManager.registerDriver(new Driver()); // from com.mysql.jdbc.Driver
    ...
    registeredDrivers.addIfAbsent(new DriverInfo(driver, da));  // mysql driver instance is added to registeredDrivers
    

    from now on JDBC knows that there is one JDBC implementation, namely mysql.
    ● Then create connection:

    Connection con = DriverManager.getConnection(url);
    

    ● Follow the source code of DriverManager.getConnection
    Pay attention to the registeredDrivers
    Here connection implementation is created by aDriver.driver.connect

    for(DriverInfo aDriver : registeredDrivers) {
    ...
         Connection con = aDriver.driver.connect(url, info);
    ...
    }
    

    ● Since connection implementation for mysql has been found, the implementation of Statement and ResultSet can also be found by digging into the source code of mysql-connector-java:5.1.36

    Statement stmt = con.createStatement();
    // String sql = "select * from user";
    ResultSet rs=stmt.executeQuery(sql);
    

    As for JDBC 4, driver implementation will be automatically detected from the classpath when the class DriverManager is loaded:
    DriverManager.java#l101

    static {
        loadInitialDrivers();
        println("JDBC DriverManager initialized");
    }
    
    ...
    
    private static void loadInitialDrivers() {
    ...
        ServiceLoader<Driver> loadedDrivers = ServiceLoader.load(Driver.class);
    ...
    }
    

    content of file mysql-connector-java-5.1.36.jar!/META-INF/services/java.sql.Driver:

    com.mysql.jdbc.Driver
    com.mysql.fabric.jdbc.FabricMySQLDriver
    
    0 讨论(0)
  • 2020-11-29 14:37

    In JDBC you first register a driver by calling

    Class.forName('classname')
    

    which loads the Database class and registers that class with DriverManager

    When you say DriverManager.getConnection() - It returns you java.sql.Connection (the contract as per specification)

    Which class implements these methods?

    The actual implementation is provided by the database vendor, for e.g. Oracle, MySQL.

    Why it is called as connection object instead of implemented class object?

    Because you code to Interface and not implementation (good coding practice).

    If you want you can look up in the vendor jar and find which class implements Connection then instead of

    Connection connection = DriverManager.getConnection()
    

    you can write

    VendorConnectionImpl vendorConnection = (VendorConnectionImpl)DriverManager.getConnection()
    

    This above will work but then it will bind you with that specific implementation.

    If you want to move from vendor1 to vendor2 you cannot do that, first you will have to change the above code as per vendor2 API, But if you use the first approach you can move from Vendor to Vendor without having pain of changing your code.

    0 讨论(0)
  • 2020-11-29 14:43

    You can't instantiate an interface class. You'll have either implement that interface yourself or use Proxy to create an instance of said interface that will delegate all calls to provided InvocationHandler.

    0 讨论(0)
  • 2020-11-29 14:43

    These guys are answering your question but I don't think that you are getting it from the comments.

    You need to actually implement the interface by creating your own class. So create a class that implements the interface, include the correct abstract methods, and then add whatever you want to the functionality in your new class. Then you'll instantiate that class to use the functions.

    An interface is like a programming "contract" it's there to make sure that any class used that extends the interface fulfills certain functions, however, you can't instantiate the interface itself to use its' methods, you inherit them/override them in your new class. After you override the abstract methods in your new class you can program in whatever else you want your particular interface extension class to do.

    http://www.javabeginner.com/learn-java/java-abstract-class-and-interface and http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html will let you read up on these topics.

    0 讨论(0)
  • 2020-11-29 14:49

    You don't actually do

    Connection conn = new Connection();
    

    You do something like

    Connection conn = DriverManager.getConnection(
         DB_PATH + "?user=" + USER_NAME + "&password=" + USER_PASS); 
    Statement statement = conn.createStatement();
    ResultSet resultSet = statement.executeQuery(query);
    

    But to answer your question directly No you cannot instantiate an Interface you have to use a class that implements the Interface or use a Proxy.

    0 讨论(0)
提交回复
热议问题