根據javadoc,createStatement()
方法會創建一個Statement
實例來向數據庫發送SQL語句。createStatement()方法如何返回Statement對象?
現在Statement
是interface
下的java.sql
包,我的理解是,不可能在Java中創建接口的實例。
那麼它是如何工作的? 在源代碼中,我發現這一點,只是我不明白。
/**
* Creates a <code>Statement</code> object for sending
* SQL statements to the database.
* SQL statements without parameters are normally
* executed using <code>Statement</code> objects. If the same SQL statement
* is executed many times, it may be more efficient to use a
* <code>PreparedStatement</code> object.
* <P>
* Result sets created using the returned <code>Statement</code>
* object will by default be type <code>TYPE_FORWARD_ONLY</code>
* and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
* The holdability of the created result sets can be determined by
* calling {@link #getHoldability}.
*
* @return a new default <code>Statement</code> object
* @exception SQLException if a database access error occurs
* or this method is called on a closed connection
*/
Statement createStatement() throws SQLException;
conn.createStatement()並不意味着有像「新聲明()」在該方法中存在的代碼。 – Loc
createStatement返回對實現Statement接口的對象的引用。 – DwB
相關:http://stackoverflow.com/questions/7550612/in-simplest-terms-what-is-a-factory/ – BalusC