Vy Do
Vy Do

Reputation: 52576

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

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

enter image description here

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, "[email protected]");
                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?

Upvotes: 1

Views: 110

Answers (1)

Vy Do
Vy Do

Reputation: 52576

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', '[email protected]', TO_DATE('2023-02-11', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (2, 'Nguyen Bich Van', '[email protected]', TO_DATE('2023-02-12', 'yyyy-mm-dd'));
INSERT INTO "CUSTOMER" (ID, NAME, EMAIL, CREATED_DATE)
VALUES (3, 'Dao Minh Thu', '[email protected]', 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, "[email protected]");
                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();
        }
    }

}

Upvotes: 1

Related Questions