How JDBC implementation works

前端 未结 4 1878
情深已故
情深已故 2021-01-13 03:11

In JDBC, I see that Connection is an interface, which defines methods for interacting with Database.

I also know that interface contains only abstract m

4条回答
  •  清酒与你
    2021-01-13 03:46

    The whole point of defining abstract classes (or interfaces) is to have subclasses of the abstract class (or classes implementing the interface), providing an implementation for all the abstract methods of the abstract class (or of the interface). Otherwise, an abstract class or interface could never be used, and would be completely pointless.

    So, what DriverManager.getConnection does, in fact, is the following:

    return new MySqlConnection();
    

    or

    return new OracleConnection();
    

    depending on the URL (this is an oversimplified explanation. In reality, it's a bit more complex than that).

    and MySQLConnection and OracleConnection are concrete classes implementing the Connection interface:

    public class MySQLConnection implements Connection {
        ...
    }
    

提交回复
热议问题