我想在事务的帮助下在两个表中插入数据。我的查询在我的SQL中正常工作但是我不知道如何在java代码上处理它请帮忙。 我的代码如下所示
private Boolean executeInsertQuery(Connection conn, String schoolID, String branchID, String studentName, String parentName, String emailId, String password, String className, String section, int age, String dob, String scholarNo, String address, String contactNo, int rollType) throws SQLException {
Boolean isSuccess = false;
String statement = "START TRANSACTION;\n" +
"INSERT INTO child_details (SCHOLAR_NUMBER, SCHOOL_ID,BRANCH_ID,CHILD_NAME,ENROLLED_CLASS," +
"CHILD_SECTION,CHILD_AGE,CHILD_DOB) VALUES (?,?,?,?,?,?,?,?);\n" +
"INSERT INTO parents_details (EMAIL_ID, BRANCH_ID,SCHOOL_ID,CHILD_NAME,SCHOLAR_NUMBER,PARENT_CONTACT_NUMBER," +
"PASSWORD,ROLE_TYPE,PARENT_NAME,ADDRESS) VALUES (?,?,?,?,?,?,?,?,?,?);\n" +
"COMMIT";
PreparedStatement stmt = null;
PreparedStatement statement1 = null;
try {
stmt = conn.prepareStatement(statement);
stmt.setString(1, scholarNo);
stmt.setString(2, schoolID);
stmt.setString(3, branchID);
stmt.setString(4, studentName);
stmt.setString(5, className);
stmt.setString(6, section);
stmt.setInt(7, age);
stmt.setString(8, dob);
stmt.setString(9,emailId);
stmt.setString(10, branchID);
stmt.setString(11, schoolID);
stmt.setString(12, studentName);
stmt.setString(13, scholarNo);
stmt.setString(14, contactNo);
stmt.setString(15, password);
stmt.setInt(16, rollType);
stmt.setString(17, parentName);
stmt.setString(18, address);
int count = stmt.executeUpdate();
// int count2 = statement1.executeUpdate();
if (count > 0) {
isSuccess = true;
} else {
isSuccess = false;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
stmt.close();
conn.close();
}
}
return isSuccess;
}
private Connection getConnection() {
String url;
Connection conn = null;
try {
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://############?#######";
} else {
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://localhost:3306/#######";
}
conn = DriverManager.getConnection(url, "root", "******");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
如何在这种情况下使用Prepared语句?
答案 0 :(得分:3)
提交/回滚事务遵循以下模式:
conn.setAutocommit(false); // No commit per statement
try (PreparedStatement stmt1 = conn.prepareSteatement(...)) { // Automatic close.
...
try (PreparedStatement stmt2 = ... )) {
...
conn.commit();
}
} catch (SQLException ex) {
conn.rollback();
}
Try-with-resources是一种奇怪的语法,简化了原本需要的结束代码。
可能需要自动生成的密钥(getGeneratedKeys
)才能从第一个语句中获取并插入第二个语句 - 比如一个具有AUTOINCR字段。搜索将提供示例代码。
答案 1 :(得分:1)
这是我要做的。根据我过去的经验,使用多个insert语句,您可以使用批处理,多个预准备调用语句或可调用语句。使用最后一个想法,这是您的代码的样子:
private Boolean executeInsertQuery(Connection conn, String schoolID, String branchID,
String studentName, String parentName, String emailId,
String password, String className, String section,
int age, String dob, String scholarNo, String address,
String contactNo, int rollType) throws SQLException {
boolean isSuccess = false;
String getDBUSERByUserIdSql = "{call insertChildAndParentDetails(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}";
try (CallableStatement callableStatement = conn.prepareCall(getDBUSERByUserIdSql)) {
callableStatement.setString(1, scholarNo);
callableStatement.setString(2, schoolID);
callableStatement.setString(3, branchID);
callableStatement.setString(4, studentName);
callableStatement.setString(5, className);
callableStatement.setString(6, section);
callableStatement.setInt(7, age);
callableStatement.setString(8, dob);
callableStatement.setString(9, emailId);
callableStatement.setString(10, branchID);
callableStatement.setString(11, schoolID);
callableStatement.setString(12, studentName);
callableStatement.setString(13, scholarNo);
callableStatement.setString(14, contactNo);
callableStatement.setString(15, password);
callableStatement.setInt(16, rollType);
callableStatement.setString(17, parentName);
callableStatement.setString(18, address);
isSuccess = (callableStatement.executeUpdate() > 0);
} catch (SQLException e) {
System.out.println("## we have an exception" + e.getMessage());
}
return isSuccess;
}
在数据库脚本中,创建上面在java代码中引用的存储过程。以下是使用oracle DB的示例(注意:我不知道您的数据库结构,但根据需要添加/删除/更改参数):
CREATE OR REPLACE PROCEDURE insertChildAndParentDetails(
p_schoolID IN childTable.SCHOOL_ID%TYPE,
p_studentName IN parentTable.STUDENTNAME%TYPE,
p_parentName IN childTable.PARENTNAME%TYPE,
p_emailId IN childTable.EMAILID%TYPE)
IS
BEGIN
INSERT INTO childTable ("SCHOOL_ID", "PARENTNAME", "EMAILID")
VALUES (p_schoolID, p_parentName,p_emailId);
INSERT INTO parentTable ("STUDENTNAME")
VALUES (p_studentName)
COMMIT;
END;
/
如果你真的想要使用多个准备好的语句,请将你的方法分成两个不同的行为,并从另一个方法中调用它们,尽管我个人会远离它:
public void insertData() {
insertChildData();
insertParentData();
}
虽然,只要确保你在这里正确地执行你的逻辑,以确保导致一个方法中的异常的事务与另一个方法同步。