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 used to run methods like createStatement(),executeQuery(),next() etc.Which class implements these methods?
Why it is called as connection object instead of implemented class object?
In JDBC, to connect and execute statements in DB we mainly make use of
Share
In JDBC you first register a driver by calling
which loads the Database class and registers that class with
DriverManagerWhen you say
DriverManager.getConnection() – It returns youjava.sql.Connection(the contract as per specification)The actual implementation is provided by the database vendor, for e.g. Oracle, MySQL.
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
you can write
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.