我了解到Connection是一个接口(interface),我们只能在接口(interface)中定义方法定义。 那么这是如何工作的:
...
语句 stmt = con.createStatement();
...
这个方法如何创建一个Statement对象并返回它?
最佳答案
因为当您调用 getConnection()
时,会返回 Connection
接口(interface)的具体实现。该接口(interface)仅定义方法签名。具体实现包含方法实现。
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println(connection.getClass().getName()); // That's the name of the concrete implementation.
它是包含这些具体实现的 JDBC 驱动程序。 JDBC API 使您能够编写独立于所使用的特定数据库服务器的 Java 代码。每当您切换数据库时,您只需切换 JDBC 驱动程序(只要 SQL 语句包含数据库服务器特定的 SQL 语言,但不包含 Java 代码,也可能会更改 SQL 语句)。
关于java - jdbc中的createStatement方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4805437/