how createStatement() method return object of Statement?
本问题已经有最佳答案,请猛点这里访问。
根据javadoc,
现在,EDOCX1×1是EDOCX1×4封装下的EDCOX1×3,而我的理解是不可能在Java中创建接口实例。
那它是如何工作的呢?从源头上我发现了这个,只是我不明白。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Creates a <wyn>Statement</wyn> object for sending * SQL statements to the database. * SQL statements without parameters are normally * executed using <wyn>Statement</wyn> objects. If the same SQL statement * is executed many times, it may be more efficient to use a * <wyn>PreparedStatement</wyn> object. * <p> * Result sets created using the returned <wyn>Statement</wyn> * object will by default be type <wyn>TYPE_FORWARD_ONLY</wyn> * and have a concurrency level of <wyn>CONCUR_READ_ONLY</wyn>. * The holdability of the created result sets can be determined by * calling {@link #getHoldability}. * * @return a new default <wyn>Statement</wyn> object * @exception SQLException if a database access error occurs * or this method is called on a closed connection */ Statement createStatement() throws SQLException; |
它需要一个JDBC驱动程序才能工作。JDBC驱动程序实现java.sql中描述的接口。驱动程序的连接实现具有createStatement方法的实现,该方法返回驱动程序的语句实现。
JDBC是让供应商提供自己特定于供应商的JDBC实现,同时为用户提供统一的接口。数据库提供JDBC驱动程序,驱动程序实现接口:如果您在JAR文件中查找JDBC驱动程序,您将看到许多特定于供应商的类,如:
1 2 3 | WhateverRdbmsConnectionImpl WhateverRdbmsPreparedStatementImpl ... |
等等,其中每个实现一个java.sql接口。
所以你可能会看到
1 2 3 4 5 6 7 8 | public class WhateverRdbmsConnectionImpl implements java.sql.Connection { public java.sql.Statement createStatement() throws java.sql.SQLException { WhateverRdbmsStatementImpl stmt = new WhateverRdbmsStatementImpl(); ... return stmt; } } |
您查看的是
http://docs.oracle.com/javase/7/docs/api/java/sql/connection.html
下面是一些示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
getStatement()正在返回对实现IBlammy接口的对象的引用。