我的环境:Windows 11 x64、Oracle数据库21c Express edtion、Java/JDK 19、IntelliJ IDEA 2023.x。我有
import java.sql.Connection;
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);
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();
}
}
}
为什么设置connection.setAutoCommit(false),插入,然后回滚,仍然将数据插入数据库?