JDBC connection [duplicate]

2019-06-13 15:00发布

问题:

This question already has an answer here:

  • JDBC- Implementation of interfaces 4 answers

Hi folks I am new to java programming.Can anyone suggest me some good reading for JDBC connection. I have to connect my java program to a database. Also while searching on net i found the following statement a bit confusing

1)Connection dbConnection=DriverManager.getConnection(url,”loginName”,”Password”)

2)Statement statement = dbConnection.createStatement();

In the second statement "createStatement" is a method of "Interface Connection".

How can i call a method of an interface??

回答1:

Ajay, JDBC is an API (Standard) published by Sun (now Oracle :-( )..

When you code, you use the API to write the code based in interfaces (NOTE: Open javadoc and see... JDBC is a collection of interfaces.. java.sql.* package)

When you want to run the application, you have to use an implementation provided by the Database vendor... All DB vendors that provide JDBC support have to provide this implementation. classes12.jar or ojdbc14.jar etc... Open that jar file in zip utility and you can see the classes java.sql.Interface and so on...

those are the concrete classes that implement the interfaces that you asked about in your question....

SO you are actually using a concrete class. Learn about inheritance in java.

these may help you

http://www.oracle.com/technetwork/java/overview-141217.html

http://download.oracle.com/javase/tutorial/jdbc/basics/index.html

http://java.sun.com/developer/onlineTraining/Database/JDBC20Intro/JDBC20.html

http://www.roseindia.net/jdbc/jdbc.shtml [good hands on tutorial]

hope this helps...

Thanks.



回答2:

In addition to the official tutorial link provided by hilal, I'll explain how JDBC works.

JDBC is a standard. It defines a set of interfaces. Then each JDBC driver - that is, a concrete implementation for a particular database, has to follow exactly these interfaces.

Then the driver registers itself in the driver manager (previously you had to do this with Class.forName(..), and when you call DriverManager.getConnection(..), the proper driver is chosen based on the passed url, and it instantiates a connection.

Now about that connection, and your last question - if you output connection.getClass() you will get com.mysql.driver.ConnectionImpl (for example). This means that the jdbc driver has provided an implementation of the Connection interface, but you don't need to be aware of the classes of each driver - you only need to know the JDBC interfaces.

That said, I think you should read a bit more not only about JDBC, but about interfaces and polymorphism in general. You refer to the object by the interface, but the object is always an instance of a concrete class that implements the interface.



回答3:

The JDBC api which is defined by the Oracle (Initially by Sun Microsystems) - - A Set of Interfaces should be implemented by the particular JDBC Vendor.

Say for example, if you are using MySql Driver, then the MySql people has to implement all the interfaces that are defined by the Oracle (Sun initially).



回答4:

It is so so simple that you can start here and simple example here



标签: java jdbc