2013-06-28 107 views
0

我正在使用Eclipse Juno,GWT,Java和MySQL。在兩個鏈接表的第二個表中創建行(通過fk鏈接)不起作用

我正在將行寫入兩個通過fk鏈接的表中。對第一個表的寫入工作正常。然後我從這個寫入中返回鍵(使用Statement.RETURN_GENERATED_KEYS)到下一個寫入。執行此寫操作時,出現錯誤「java.sql.SQLException:沒有爲參數1指定值」。我已經顯示了傳遞給每個參數的值,並且有值。

兩個表中的第一列是一個自動增量pk。第二個表中的第二列是fk。

該控制檯是(顯示在第一五行是被傳遞到的參數值和第一個是FK):

cd_id = 6 
section = Cubs 
pack = Explorer 
start date = 2013-06-28 
end date = 2013-06-28 
SQLException createYouthMember 1. 
java.sql.SQLException: No value specified for parameter 1 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) 
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2611) 
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2586) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2432) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375) 
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359) 
at org.AwardTracker.server.MySQLConnection.createYouthMember(MySQLConnection.java:342) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561) 
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208) 
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248) 
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) 
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362) 
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) 
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) 
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729) 
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) 
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) 
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49) 
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) 
at org.mortbay.jetty.Server.handle(Server.java:324) 
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505) 
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843) 
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647) 
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:205) 
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380) 
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395) 
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488) 

發展模式是:

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; 
} 

而這些表格是:

at_cub_details table

at_section_details table

foreign key setup

我希望得到任何幫助。問候,格林

回答

1

ps應該ps2

ps2.setInt(1, cd_id); 
ps2.setString(2, section); 
ps2.setString(3, pack); 
ps2.setDate(4, (java.sql.Date) startDate); 
ps2.setDate(5, (java.sql.Date) endDate); 
+1

請了解如何格式化代碼 – peterm

+0

謝謝peterm,我的道歉浪費大家的時間,這樣一個愚蠢的錯誤,我的對你的幫助表示衷心感謝。問候,格林。 – Glyn

相關問題