2016-02-27 56 views
3
不工作

我創建表是這樣的:rs.getGeneratedKeys()在德比

public final String CREATE_QUESTIONS_TABLE = "CREATE TABLE Questions(QuestionID INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY," 
     + "SubmittedTime TIMESTAMP NOT NULL," 
     + "Title VARCHAR(50) NOT NULL," 
     + "Text VARCHAR(300) NOT NULL," 
     + "Username VARCHAR(10) NOT NULL," 
     + "Rating INTEGER DEFAULT 0 NOT NULL)"; 

我的INSERT查詢看起來是這樣的:

servlet代碼的
public final String INSERT_QUESTION_STMT = "INSERT INTO Questions(SubmittedTime, Title, Text, Username) VALUES(?,?,?,?)"; 

部分,其中我插入的樣子這個:

// insert question into database 
PreparedStatement pstmt = conn.prepareStatement(AppConstants.INSERT_QUESTION_STMT); 
pstmt.setTimestamp(1, new Timestamp(System.currentTimeMillis())); 
pstmt.setString(2, newQuestion.getTitle()); 
pstmt.setString(3, newQuestion.getText()); 
pstmt.setString(4, (String)session.getAttribute("username")); 
pstmt.executeUpdate(); 

到目前爲止一切正常,它將問題插入到數據庫中。

我想問題的自動生成的ID,但這樣我嘗試使用:

ResultSet rs = pstmt.getGeneratedKeys(); 
int id = -1; 
if(rs.next()) 
id = rs.getInt(1); 

我得到了rs.next()

SEVERE: Servlet.service() for servlet [AskQuestion] in context with path [/QnA] threw exception 
java.lang.NullPointerException 
at QnA.servlets.AskQuestion.doPost(AskQuestion.java:78) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Unknown Source) 

隨機誤差的任何想法,爲什麼它不起作用?

回答

5

我找到了答案,我不得不告訴聲明返回生成的密鑰。

PreparedStatement pstmt = conn.prepareStatement(AppConstants.INSERT_QUESTION_STMT, 
     Statement.RETURN_GENERATED_KEYS); 

注意Statement.RETURN_GENERATED_KEYS作爲函數的第二個參數。

+0

感謝編輯+ jdbc主題(它仍然可以與PreparedStatement.RETURN_GENERATED_KEYS一起工作,但是有更好的選擇) – ImZyzzBrah

+0

這是因爲'PreparedStatement'擴展了'Statement',所以它也繼承了定義的常量。使用'Statement'作爲常量是首選,因爲1)這是常量被實際定義的地方,2)JDBC標準,文檔等使用'Statement.RETURN_GENERATED_KEYS'。 –

+0

感謝您的澄清 – ImZyzzBrah