谁在这个 java 程序中实现了 Connection、ResultSet 和 Statement 接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17136435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Who implements Connection, ResultSet and Statement interfaces in this java program?
提问by Cyclops
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 ?
最近发现下面程序中用到的Statement、Connection、ResultSet都是接口。这个程序完全正常,但谁实现了这些接口?
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));
}
}
}
回答by Sandeep Patange
Interfaces Statement, Connection and the ResultSet are finally implemented by the third party JDBC Driver provider.
接口 Statement、Connection 和 ResultSet 最终由第三方 JDBC Driver 提供程序实现。
Suppose if you are using MySQL Driver, this is how the hierarchy of implementation follows,
假设您正在使用 MySQL 驱动程序,这就是实现层次结构遵循的方式,
- java.sql.Statement interface is finally implemented by com.mysql.jdbc.StatementImpl Class.
- java.sql.Statement 接口最终由 com.mysql.jdbc.StatementImpl 类实现。
Internal implementation hierarchy:
内部实现层次:
com.mysql.jdbc.StatementImpl(Class) -->implements--> com.mysql.jdbc.Statement(interface) -->extends--> java.sql.Statement(interface)
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.
- java.sql.Connection 接口最终由 com.mysql.jdbc.JDBC4Connection 类实现。
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)
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.
- java.sql.ResultSet 接口最终由 com.mysql.jdbc.ResultSetImpl 类实现。
Internal implementation hierarchy:
内部实现层次:
com.mysql.jdbc.ResultSetImpl(Class) -->implements--> com.mysql.jdbc.ResultSetInternalMethods(Interface) -->extends--> java.sql.ResultSet(Interface)
com.mysql.jdbc.ResultSetImpl(Class) -->implements--> com.mysql.jdbc.ResultSetInternalMethods(Interface) -->extends--> java.sql.ResultSet(Interface)
回答by Kartik73
The JDBC API is implemented through the JDBC driver which are being provided by the various Database software vendors.
JDBC API 是通过各种数据库软件供应商提供的 JDBC 驱动程序实现的。
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
JDBC 驱动程序是一组实现 JDBC 接口以处理 JDBC 调用并将结果集返回给 Java 应用程序的类
回答by Chintan Patel
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:
这些类型的接口的实现是特定于供应商的。例如,如果您使用的是 Mysql 并且您已将 Mysql Jar 放在类路径中,则实现类如下所示:
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]
回答by Peter Lawrey
The JDBC driver provider provides the implementations you can do this. In this case Oracle.
JDBC 驱动程序提供程序提供了您可以执行此操作的实现。在这种情况下,甲骨文。
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.
顺便说一句,当你完成它们时不要忘记关闭你的资源,否则你可以获得记忆力。
回答by Debpratim Ghosh
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 interfacesand writes their body according to the requirement. so the body of the drivers is changed when u move from mysql database to oracle database.
数据库供应商提供 3rd 方驱动程序,JDBC-ODBC 驱动程序通过这些驱动程序与数据库进行通信。这些第 3 方驱动程序具有某些实现接口并根据要求编写其主体的类。所以当你从 mysql 数据库移动到 oracle 数据库时,驱动程序的主体会发生变化。