我創建了一個到mysql數據庫的連接。下面是我的代碼界面如何可以引用沒有實現接口的對象?
package org;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
public class DatabaseConn {
public static void main(String[] args) {
System.out.println("Loading driver...");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot find the driver in the classpath!", e);
}
try {
String host = "jdbc:mysql://localhost:3306/sys";
String username = "root";
String password = "root";
Connection Con = DriverManager.getConnection(host, username, password);
Statement stmnt = Con.createStatement();
String SQL = "SELECT * FROM sys_config";
ResultSet rs = stmnt.executeQuery(SQL);
System.out.println("Records:"+rs);
} catch (SQLException err) {
System.out.println(err.getMessage());
}
}
}
我對類的接口實現的理解說,接口類型引用實現接口的類的對象。
但是,當我調查低於上述的代碼中使用的代碼片斷..
Connection Con = DriverManager.getConnection(host, username, password);
的DriverManager.getConnection(主機,用戶名,密碼)返回(類型連接的)一個參照本發明的對象,但沒有接口被實現在類DriverManager中。任何人都可以清除我的這個懷疑..?或者我錯過了什麼?
同樣的事情,我不能夠得到下面的代碼片斷
Statement SQL = Con.createStatement();
Con.createStatement()應參考返回實現Statement接口一個對象。但這種連接接口由ConnectionImpl類實現,其執行存在類似下面
public class ConnectionImpl
extends ConnectionPropertiesImpl
implements Connection {
public Statement createStatement()
throws SQLException
{
return createStatement(1003, 1007);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException
{
checkClosed();
StatementImpl stmt = new StatementImpl(this, this.database);
stmt.setResultSetType(resultSetType);
stmt.setResultSetConcurrency(resultSetConcurrency);
return stmt;
}
}
很明顯'StatementImpl'是一個實現'Statement'的類,'createStatement()'返回該類的一個實例。 – Eran