简体   繁体   English

我们如何在jdbc中使用prepare语句,因为它是一个接口

[英]how can we use prepare statement in jdbc since it is an interface

In JDBC except DriverManager all are interfaces. 在JDBC中,除了DriverManager之外,所有都是接口。 How are we able to use the interfaces such as, PrepareStatement , Connection etc.? 我们如何使用诸如PrepareStatementConnection等接口?

The drivermanager returns a concrete implementation of the Connection . 驱动程序管理器返回Connection的具体实现。 The concrete implementation of the Connection in turn returns a concrete implementation of the Statement . Connection的具体实现又返回Statement的具体实现。 The concrete implementation of the Statement in turn returns a concrete implementation of the ResultSet . Statement的具体实现又返回ResultSet的具体实现。 And so on. 等等。

Those concrete implementations are provided by the JDBC driver. 这些具体的实现由JDBC驱动程序提供。 To learn about the underlying implementation, do a 要了解底层实现,请执行

System.out.println(connection.getClass());

You'll see that it's not java.sql.Connection , but just the one provided by the JDBC driver used. 您将看到它不是java.sql.Connection ,而是所使用的JDBC驱动程序提供的那个。

Interfaces are just there to define a contract the implementor has to adhere. 接口只是用来定义实现者必须遵守的契约。 This enables you to reuse the same JDBC code with different JDBC drivers without the need to rewrite the code (expect of maybe DB specific SQL queries, but that's not a Java/JDBC problem) whenever you'd like to reuse the same code on a different DB server. 这样一来,只要您想在服务器上重用相同的代码,就可以在无需重写代码的情况下重用相同的JDBC代码,而不必重写代码(可能是特定于数据库的SQL查询,但这不是Java / JDBC问题)。不同的数据库服务器。

DriverManager : DriverManager:

  1. download the JDBC driver 下载JDBC驱动程序

  2. put it in your JAVA projet 将其放在您的JAVA projet中

  3. define path to the jar Driver in projet properties 在projet属性中定义jar驱动程序的路径

Example of Connection : 连接示例:

public static void exampleOfConnection() {  
    try {
        /* How to instanciate a connection with a specific driver manager */
        Class.forName("org.sqlite.JDBC");
        Connection connectionDB = DriverManager.getConnection("jdbc:sqlite:~/workspace/POEC/Data/films.sqlite");
        connectionDB.setAutoCommit(false);

        /* Begin of statements */
        /* Here are use statements */
        /* End of statements*/

        connectionDB.commit();
        connectionDB.close();

    } catch (ClassNotFoundException | SQLException e) {
        e.printStackTrace();
    }
}

Example of Statement : 声明示例:

public static void exampleOfStatementTest(Connection conn) throws SQLException {
    String createFilmsTableQuery = "CREATE  TABLE  IF NOT EXISTS films" + "("
            + " filmId INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE ," + " filmTitle TEXT,"
            + " filmDirector TEXT," + " filmType TEXT," + " filmYear INTEGER " + ");";
    try (Statement statement = conn.createStatement()) {
        statement.execute(createFilmsTableQuery);
        statement.close();
    }
}

Example of PrepareStatement : PrepareStatement的示例

private static void exampleOfPrepareStatementTest(Connection conn) throws SQLException {
    String insertIntoFilmsPrepareStatementQuery = "INSERT INTO films "
            + "(filmTitle, filmDirector, filmType, filmyear) " + "VALUES (? ,? ,? ,? )";
    try (Statement st = conn.createStatement()) {
        PreparedStatement preparedStatement = conn.prepareStatement(insertIntoFilmsPrepareStatementQuery);
        preparedStatement.setString(1, "Jackie Brown");
        preparedStatement.setString(2, "Tarentino");
        preparedStatement.setString(3, "detective film");
        preparedStatement.setInt(4, 1997);
        int filmAddedQuantity = preparedStatement.executeUpdate();
        System.out.println(filmAddedQuantity + " Film(s) added");
        preparedStatement.close();
        st.close();
    }
}

The DriverManager (or DataSource ) is your starting point -- get the Connection etc. from it. DriverManager(或DataSource )是您的起点-从中获取Connection等。 You don't need to directly instantiate any of these. 您无需直接实例化任何这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM