问题
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));
}
}
}
回答1:
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,
- 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)
- 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)
- 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)
回答2:
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
回答3:
The JDBC driver provider provides the implementations you can do this. In this case Oracle.
System.out.println("con class is "+ con.getClass());
System.out.println("comm class is "+ comm.getClass());
System.out.println("res class is "+ res.getClass());
BTW don't forget to close off your resources when you have finished with them or you can get a memory elak.
回答4:
Implementation of these type of interface is vendor specific. For example if you are using Mysql and you have put Mysql Jar in classpath then Implementation classes are as per following:
1. java.sql.Statement [Interface] -> com.mysql.jdbc.StatementImpl [Class]
2. java.sql.Connection [Interface] -> com.mysql.jdbc.MySQLConnection [Interface] -> com.mysql.jdbc.ConnectionImpl [Class]
3. java.sql.ResultSet [Interface] -> com.mysql.jdbc.ResultSetImpl [Class]
回答5:
The database vendors provide the 3rd party drivers through which JDBC-ODBC driver communicates with the database. these 3rd party drivers have certain classes that implements the interfaces and writes their body according to the requirement. so the body of the drivers is changed when u move from mysql database to oracle database.
来源:https://stackoverflow.com/questions/17136435/who-implements-connection-resultset-and-statement-interfaces-in-this-java-progr