英文:
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证书,我读了一本书,其中我看到了以下内容:
来源: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();
}
}
}
但我看到的概念仍然不太清楚。Driver
、PreparedStatement
的java.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
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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论