Driver, PreparedStatement的类/接口在Oracle的ojdbc11.jar中是什么?

huangapple go评论80阅读模式
英文:

What class/interface are Driver, PreparedStatement's implementation of java.sql.DriverManager , java.sql.PreparedStatement in Oracle's ojdbc11.jar?

问题

我的开发环境: Java / JDK 19,Oracle数据库 21c Express Edition。我正在学习OCP 17证书,我读了一本书,其中我看到了以下内容:

Driver, PreparedStatement的类/接口在Oracle的ojdbc11.jar中是什么?

来源:Jeanne Boyarsky, Scott Selikoff . https://www.wiley.com/en-us/OCP+Oracle+Certified+Professional+Java+SE+17+Developer+Study+Guide%3A+Exam+1Z0+829-p-9781119864585

我从https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html和https://download.oracle.com/otn-pub/otn_software/jdbc/218/ojdbc11.jar?AuthParam=1675837769_3641dc4062ae318b752c22338c697ca4 下载了JDBC,并添加到依赖项中。然后,我通过一个示例进行了练习:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class VyUpdate {

    public static void main(String[] args) {
        try (Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "12345678")) {
            if (connection != null) {
                System.out.println("已连接到数据库。");
                String query = "update SYSTEM.CUSTOMER set email = ? where id = ?";
                PreparedStatement preparedStatement = connection.prepareStatement(query);
                preparedStatement.setString(1, "anhtrangmuathu222@gmail.com");
                preparedStatement.setInt(2, 3);
                preparedStatement.executeUpdate();
            } else {
                System.out.println("无法建立连接。");
            }
        } catch (SQLException sqlException) {
            System.err.format("SQL状态: %s\n%s", sqlException.getSQLState(), sqlException.getMessage());
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}

但我看到的概念仍然不太清楚。DriverPreparedStatementjava.sql.DriverManager的实现类/接口,以及Oracle的ojdbc11.jar中的java.sql.PreparedStatement是什么类/接口?

英文:

My development environment: Java / JDK 19, Oracle database 21c express edition. I am studying OCP 17 certificate, I read book where I seen

Driver, PreparedStatement的类/接口在Oracle的ojdbc11.jar中是什么?

Source: Jeanne Boyarsky, Scott Selikoff . https://www.wiley.com/en-us/OCP+Oracle+Certified+Professional+Java+SE+17+Developer+Study+Guide%3A+Exam+1Z0+829-p-9781119864585

I downloaded JDBC at https://www.oracle.com/database/technologies/appdev/jdbc-downloads.html and https://download.oracle.com/otn-pub/otn_software/jdbc/218/ojdbc11.jar?AuthParam=1675837769_3641dc4062ae318b752c22338c697ca4 , add to dependencie., Then, I practices by an example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class VyUpdate {

    public static void main(String[] args) {
        try (Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "12345678")) {
            if (connection != null) {
                System.out.println("Connected to the database.");
                String query = "update SYSTEM.CUSTOMER set email = ? where id = ?";
                PreparedStatement preparedStatement = connection.prepareStatement(query);
                preparedStatement.setString(1, "anhtrangmuathu222@gmail.com");
                preparedStatement.setInt(2, 3);
                preparedStatement.executeUpdate();
            } else {
                System.out.println("Failed to make connection.");
            }
        } catch (SQLException sqlException) {
            System.err.format("SQL State: %s\n%s", sqlException.getSQLState(), sqlException.getMessage());
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}

but the idea I what seen still not clear. What class/interface are Driver, PreparedStatement's implementation of java.sql.DriverManager , java.sql.PreparedStatement in Oracle's ojdbc11.jar?

答案1

得分: 1

以下是您要翻译的代码部分:

System.out.println(preparedStatement.getClass().getName());
// Result: oracle.jdbc.driver.OraclePreparedStatementWrapper

DriverManager.getDriver("jdbc:oracle:thin:@localhost:1521:XE");
System.out.println(foo.getClass().getName());
// Result: oracle.jdbc.OracleDriver
CREATE TABLE CUSTOMER
(
    ID           NUMBER(10) NOT NULL,
    NAME         VARCHAR2(100) NOT NULL,
    EMAIL        VARCHAR2(100) NOT NULL,
    CREATED_DATE DATE NOT NULL,
    CONSTRAINT CUSTOMER_PK PRIMARY KEY (ID)
);

INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (1, 'Tran Phuong Ly', 'lylychancute@outlook.com', TO_DATE('2023-02-11', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (2, 'Nguyen Bich Van', 'bichvantt@yahoo.com', TO_DATE('2023-02-12', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (3, 'Dao Minh Thu', 'thudm@yahoo.com', TO_DATE('2023-02-13', 'yyyy-mm-dd'));
COMMIT;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.ZoneId;

public class VyInsert {

    public static void main(String[] args) {
        try (Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "12345678")) {

            if (connection != null) {
                connection.setAutoCommit(false);
                System.out.println("Connected to the database.");
                String query = "insert into SYSTEM.CUSTOMER (ID, NAME, EMAIL, CREATED_DATE) values (?, ?, ?, ?)";
                PreparedStatement preparedStatement = connection.prepareStatement(query);
                Driver foo = DriverManager.getDriver("jdbc:oracle:thin:@localhost:1521:XE");
                System.out.println(foo.getClass().getName());
                // oracle.jdbc.OracleDriver
                preparedStatement.setInt(1, 17);
                preparedStatement.setString(2, "Nguyen Thu Hang23");
                preparedStatement.setString(3, "anhtrangmuathu32@gmail.com");
                preparedStatement.setObject(4, LocalDate.now(ZoneId.of("America/Montreal")));
                // preparedStatement.setObject(4, LocalDate.now(ZoneId.of("Asia/Ho_Chi_Minh")));
                preparedStatement.executeUpdate();
                // connection.commit();
                //connection.setAutoCommit(true);
            } else {
                System.out.println("Failed to make connection.");
            }

        } catch (SQLException sqlException) {
            System.err.format("SQL State: %s\n%s", sqlException.getSQLState(), sqlException.getMessage());
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}
英文:

Use like this

System.out.println(preparedStatement.getClass().getName());
// Result: oracle.jdbc.driver.OraclePreparedStatementWrapper

DriverManager.getDriver("jdbc:oracle:thin:@localhost:1521:XE");
System.out.println(foo.getClass().getName());
// Result: oracle.jdbc.OracleDriver

Full working sample, with Oracle database 21c Express edition, Java/JDK 19:

SQL

CREATE TABLE CUSTOMER
(
    ID           NUMBER(10) NOT NULL,
    NAME         VARCHAR2(100) NOT NULL,
    EMAIL        VARCHAR2(100) NOT NULL,
    CREATED_DATE DATE NOT NULL,
    CONSTRAINT CUSTOMER_PK PRIMARY KEY (ID)
);

INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (1, 'Tran Phuong Ly', 'lylychancute@outlook.com', TO_DATE('2023-02-11', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (2, 'Nguyen Bich Van', 'bichvantt@yahoo.com', TO_DATE('2023-02-12', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (3, 'Dao Minh Thu', 'thudm@yahoo.com', TO_DATE('2023-02-13', 'yyyy-mm-dd'));
COMMIT;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.ZoneId;

public class VyInsert {

    public static void main(String[] args) {
        try (Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "12345678")) {

            if (connection != null) {
                connection.setAutoCommit(false);
                System.out.println("Connected to the database.");
                String query = "insert into SYSTEM.CUSTOMER (ID, NAME, EMAIL, CREATED_DATE) values (?, ?, ?, ?)";
                PreparedStatement preparedStatement = connection.prepareStatement(query);
                Driver foo = DriverManager.getDriver("jdbc:oracle:thin:@localhost:1521:XE");
                System.out.println(foo.getClass().getName());
                // oracle.jdbc.OracleDriver
                preparedStatement.setInt(1, 17);
                preparedStatement.setString(2, "Nguyen Thu Hang23");
                preparedStatement.setString(3, "anhtrangmuathu32@gmail.com");
                preparedStatement.setObject(4, LocalDate.now(ZoneId.of("America/Montreal")));
                // preparedStatement.setObject(4, LocalDate.now(ZoneId.of("Asia/Ho_Chi_Minh")));
                preparedStatement.executeUpdate();
                // connection.commit();
                //connection.setAutoCommit(true);
            } else {
                System.out.println("Failed to make connection.");
            }

        } catch (SQLException sqlException) {
            System.err.format("SQL State: %s\n%s", sqlException.getSQLState(), sqlException.getMessage());
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

}

huangapple
  • 本文由 发表于 2023年2月8日 16:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定