有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java createStatement()方法如何返回语句的对象?

根据javadoc,createStatement()方法创建一个Statement实例,用于向数据库发送SQL语句

现在Statementjava.sql包下的interface,我的理解是,不可能在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;

共 (2) 个答案

  1. # 1 楼答案

    下面是一些示例代码:

    interface IBlammy
    {
        public String blam();
    }
    
    class BlammyImpl
    implements IBlammy
    {
        public String blam()
        {
            return "kapow";
        }
    }
    
    
    class Hooty
    {
        public IBlammy getStatement()
        {
            return new BlammyImpl();
        }
    }
    

    胡闹。getStatement()返回对实现IBlammy接口的对象的引用