Java 在两个链接表(通过fk链接)的第二个表中创建行不起作用
我正在使用EclipseJuno、GWT、java和MySQL 我正在将行写入通过fk链接的两个表。对第一个表的写入工作正常。然后,我将此写入的密钥(使用语句返回生成的密钥)返回到下一次写入。执行此写操作时,我收到一个错误“java.sql.SQLException:没有为参数1指定值”。我已经显示了传递给每个参数的值,并且有值 两个表中的第一列都是自动递增主键。第二个表中的第二列是fk 控制台为(显示的前五行是传递给参数的值,第一行是fk): 发展模式是:Java 在两个链接表(通过fk链接)的第二个表中创建行不起作用,java,mysql,eclipse,gwt,Java,Mysql,Eclipse,Gwt,我正在使用EclipseJuno、GWT、java和MySQL 我正在将行写入通过fk链接的两个表。对第一个表的写入工作正常。然后,我将此写入的密钥(使用语句返回生成的密钥)返回到下一次写入。执行此写操作时,我收到一个错误“java.sql.SQLException:没有为参数1指定值”。我已经显示了传递给每个参数的值,并且有值 两个表中的第一列都是自动递增主键。第二个表中的第二列是fk 控制台为(显示的前五行是传递给参数的值,第一行是fk): 发展模式是: 13:16:16.910 [WARN
13:16:16.910 [WARN] [org.AwardTracker.AwardTracker] Something other than an int was returned from JSNI method '@com.google.gwt.dom.client.DOMImplIE9::getBoundingClientRectTop(Lcom/google/gwt/dom/client/Element;)': Rounding double (216.00999450683594) to int for int
相关服务器端代码为:
public YthMmbrSectDtls createYouthMember(String youthMemberId,
String surname, String firstname, Date dob,
String password, Date archived, String scout_no, String sectionDetailsId,
String sectionCubDetailsId, String section, String pack, Date startDate, Date endDate) {
YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
Integer cd_id = 0;
String queryCubDetailsTable =
"INSERT INTO at_cub_details "
+ "(cd_surname, cd_first_name, cd_dob, cd_archived, cd_scout_no) "
+ "VALUES (?, ?, ?, ?, ?)";
String querySectionDetailsTable =
"INSERT INTO at_section_details "
+ "(cd_id, sd_section, sd_pack, sd_start_date, sd_end_date) "
+ "VALUES (?, ?, ?, ?, ?)";
try {
ps = conn.prepareStatement(queryCubDetailsTable, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, surname);
ps.setString(2, firstname);
ps.setDate(3, (java.sql.Date) dob);
ps.setDate(4, (java.sql.Date) archived);
ps.setString(5, scout_no);
ps.executeUpdate();
ps2 = conn.prepareStatement(querySectionDetailsTable);
//Get foreign key from insert into at_cub_details
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
cd_id = rs.getInt(1);
};
System.out.println("cd_id = " + cd_id);
System.out.println("section = " + section);
System.out.println("pack = " + pack);
System.out.println("start date = " + startDate);
System.out.println("end date = " + endDate);
ps.setInt(1, cd_id);
ps.setString(2, section);
ps.setString(3, pack);
ps.setDate(4, (java.sql.Date) startDate);
ps.setDate(5, (java.sql.Date) endDate);
ps2.executeUpdate();
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException createYouthMember 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 3.");
e.printStackTrace();
}
}
if (ps2 != null) {
try {
ps2.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 4.");
e.printStackTrace();
}
}
}
return ythMmbrSectDtls;
}
表格如下:
我将感谢任何帮助。关于这一点,Glyn
ps
应该是ps2
ps2.setInt(1,cd_id);
ps2.固定管柱(第2节);
ps2.固定管柱(3,组件);
setDate(4,(java.sql.Date)startDate);
setDate(5,(java.sql.Date)endDate);
谢谢彼得,我为这样一个愚蠢的错误浪费了大家的时间而道歉,也衷心感谢你的帮助。你好,格林。
public YthMmbrSectDtls createYouthMember(String youthMemberId,
String surname, String firstname, Date dob,
String password, Date archived, String scout_no, String sectionDetailsId,
String sectionCubDetailsId, String section, String pack, Date startDate, Date endDate) {
YthMmbrSectDtls ythMmbrSectDtls = null; // necessary unless you do something in the exception handler
ResultSet result = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
Integer cd_id = 0;
String queryCubDetailsTable =
"INSERT INTO at_cub_details "
+ "(cd_surname, cd_first_name, cd_dob, cd_archived, cd_scout_no) "
+ "VALUES (?, ?, ?, ?, ?)";
String querySectionDetailsTable =
"INSERT INTO at_section_details "
+ "(cd_id, sd_section, sd_pack, sd_start_date, sd_end_date) "
+ "VALUES (?, ?, ?, ?, ?)";
try {
ps = conn.prepareStatement(queryCubDetailsTable, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, surname);
ps.setString(2, firstname);
ps.setDate(3, (java.sql.Date) dob);
ps.setDate(4, (java.sql.Date) archived);
ps.setString(5, scout_no);
ps.executeUpdate();
ps2 = conn.prepareStatement(querySectionDetailsTable);
//Get foreign key from insert into at_cub_details
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
cd_id = rs.getInt(1);
};
System.out.println("cd_id = " + cd_id);
System.out.println("section = " + section);
System.out.println("pack = " + pack);
System.out.println("start date = " + startDate);
System.out.println("end date = " + endDate);
ps.setInt(1, cd_id);
ps.setString(2, section);
ps.setString(3, pack);
ps.setDate(4, (java.sql.Date) startDate);
ps.setDate(5, (java.sql.Date) endDate);
ps2.executeUpdate();
}
catch (SQLException e) {
//do stuff on fail
System.out.println("SQLException createYouthMember 1.");
e.printStackTrace();
user = null;
}
finally {
if (result != null) {
try {
result.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 2.");
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 3.");
e.printStackTrace();
}
}
if (ps2 != null) {
try {
ps2.close();
}
catch (SQLException e) {
System.out.println("SQLException createYouthMember 4.");
e.printStackTrace();
}
}
}
return ythMmbrSectDtls;
}