【发布时间】:2016-05-25 06:34:32
【问题描述】:
这个方法很有效,只是它让我觉得我做得不对。我有 2 个与 where 相关的表
一个学科有许多学年。 (一个学科可以有多个学年,也可以属于不同的学年)
一个学年有许多科目
主题
code PK
creator
dateCreated
description
name
units
yearLevel
学年科目
id PK
code FK
dateAdded
schoolyear
方法如下。
public Boolean add(){
Boolean success ;
String SQLa = "INSERT INTO subject(name,code,units,description,yearlevel,creator) "
+ "VALUES (?,?,?,?,?,?)";
String SQLb = "INSERT INTO schoolyearsubjects(code,schoolyear,addedBy) values(?,?,?)";
try(Connection con = DBUtil.getConnection(DBType.MYSQL);
PreparedStatement ps1 = con.prepareStatement(SQLa);
PreparedStatement ps2= con.prepareStatement(SQLb);){
//a.)Prepare ps1
ps1.setString(1,subjectName );
ps1.setString(2,subjectCode );
ps1.setInt(3, subjectUnits);
ps1.setString(4, subjectDescription);
ps1.setString(5, subjectYearLevel);
ps1.setString(6, Login.getUsername());
//b.)Prepare ps2
ps2.setString(1,subjectCode);
ps2.setString(2, schoolYearStart+"-"+schoolYearEnd);
ps2.setString(3, Login.getUsername());
//c.) execute both statements
ps1.executeUpdate();
ps2.executeUpdate();
success = true;
} catch (SQLException e) {
success = false;
JOptionPane.showMessageDialog(null,e.getClass()+" "+e.getMessage());
}
return success;
}
我有点犹豫是否坚持在一个方法中包含 2 个准备好的语句。
考虑到code 列是Foreign Key 并且两者都必须在同一个函数中执行,我如何确保成功执行两个语句?
另外,要补充一点,add() 方法只绑定到一个按钮。
如果我做错了什么,请给我建议并纠正我。
谢谢。
【问题讨论】:
-
如果没有其他原因,您应该考虑将
INSERT语句分开,因为它允许在您的调用代码中单独处理故障。目前,您必须使用错误代码来区分一个故障和另一个故障,因为单个布尔值无法传达这一点。 -
执行两个preparedStatement没有错,但是请不要忘记关闭创建的
PreparedStatement和Connection对象。否则会导致 sqlError。
标签: java mysql database prepared-statement