Java 为什么即使我没有';你不承诺吗?
我有一个方法来检查commit是如何工作的。我试图设置错误的值来获取异常并在它抛出时进行检查。我原以为它会在提交之后出现,但当我使用Java 为什么即使我没有';你不承诺吗?,java,mysql,exception,jdbc,commit,Java,Mysql,Exception,Jdbc,Commit,我有一个方法来检查commit是如何工作的。我试图设置错误的值来获取异常并在它抛出时进行检查。我原以为它会在提交之后出现,但当我使用executeUpdate方法时,它会抛出。这个方法应该是这样工作的吗 public void check(){ String query = "Insert into user_info(login,userPassword,userType,userEmail)values(?,?,?,?);"; Connection con
executeUpdate
方法时,它会抛出。这个方法应该是这样工作的吗
public void check(){
String query = "Insert into user_info(login,userPassword,userType,userEmail)values(?,?,?,?);";
Connection connection = null;
PreparedStatement statement = null;
try{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/taksi_online","Bruce","givanchy");
connection.setAutoCommit(false);
statement = connection.prepareStatement(query);
statement.setString(1,"asdasqqqqqqq");
statement.setString(2,"sff");
statement.setString(3,"client");
statement.setString(4,"qweq");
System.out.println(statement.executeUpdate());
} catch (SQLException e) {
System.out.println("Error message "+e.getMessage());
System.out.println("Error code "+e.getErrorCode());
System.out.println("Sql state is "+e.getSQLState());
// I get error at this place
}finally {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
System.out.println("after");
try {
//but I thought I should get it here after commit
connection.commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}finally {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
这个方法应该是这样工作的吗
public void check(){
String query = "Insert into user_info(login,userPassword,userType,userEmail)values(?,?,?,?);";
Connection connection = null;
PreparedStatement statement = null;
try{
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/taksi_online","Bruce","givanchy");
connection.setAutoCommit(false);
statement = connection.prepareStatement(query);
statement.setString(1,"asdasqqqqqqq");
statement.setString(2,"sff");
statement.setString(3,"client");
statement.setString(4,"qweq");
System.out.println(statement.executeUpdate());
} catch (SQLException e) {
System.out.println("Error message "+e.getMessage());
System.out.println("Error code "+e.getErrorCode());
System.out.println("Sql state is "+e.getSQLState());
// I get error at this place
}finally {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
System.out.println("after");
try {
//but I thought I should get it here after commit
connection.commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
try {
connection.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}finally {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
对。您没有提交更改并不意味着您可以执行无效的更改
事务发生在数据库上,而不是在应用程序中。因此,您执行的任何操作都必须是对其中数据的有效数据库操作。事务所做的(显然过于简化)是将其全部封装在一组原子操作中。但每个操作仍然需要有效
抛出异常允许开发人员捕获异常并回滚整个事务。这就是关键所在