2017-06-20 62 views
-1
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    connection.connect.connection(); 
    response.setContentType("text/html"); 
    Integer rating; 
    String feedback, sql; 
    PreparedStatement insert; 
    RequestDispatcher reqdisp; 
    try { 
     connection.connect.con.setAutoCommit(false); 

     rating = Integer.parseInt(request.getParameter("rating")); 
     feedback = request.getParameter("feedback").trim(); 

     sql = "update Message set rating=?, feedback=? WHERE mid=LAST_INSERT_ID()"; 
     insert = connection.connect.con.prepareStatement(sql); 
     insert.setObject(1, rating); 
     insert.setObject(2, feedback); 
     insert.executeUpdate(); 
     JOptionPane.showMessageDialog(null, "Successfully inserted"); 
     connection.connect.con.commit(); 
     connection.connect.con.close();           
} catch (Exception e) { 
    System.out.println(e); 
     if (connection.connect.con != null) { 
      try { 
       connection.connect.con.rollback(); 
       connection.connect.con.close(); 
      } catch (SQLException ex) { 
       JOptionPane.showMessageDialog(null, "Error"); 
      } 
     } 
    } 
    reqdisp = getServletContext().getRequestDispatcher("/thankYou.jsp"); 
    reqdisp.forward(request, response); 

} 

此代碼不會產生任何錯誤,但表格永遠不會更新。有人能幫我在我的MySQL查詢中找出問題嗎?謝謝!從Servlet(java)到MySQL的更新表格

Jun 20, 2017 12:29:09 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Messages' did not find a matching property. 
Jun 20, 2017 12:29:09 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:TestingJSP' did not find a matching property. 
Jun 20, 2017 12:29:09 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JQueryAjaxDemo' did not find a matching property. 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server version:  Apache Tomcat/9.0.0.M21 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server built:   May 4 2017 22:42:36 UTC 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server number:   9.0.0.0 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: OS Name:    Mac OS X 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: OS Version:   10.12.5 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Architecture:   x86_64 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Java Home:    /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: JVM Version:   1.8.0_131-b11 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: JVM Vendor:   Oracle Corporation 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: CATALINA_BASE:   /Users/rosemary/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: CATALINA_HOME:   /opt/apache-tomcat-9.0.0.M21 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcatalina.base=/Users/rosemary/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcatalina.home=/opt/apache-tomcat-9.0.0.M21 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dwtp.deploy=/Users/rosemary/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.endorsed.dirs=/opt/apache-tomcat-9.0.0.M21/endorsed 
Jun 20, 2017 12:29:09 PM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dfile.encoding=UTF-8 
Jun 20, 2017 12:29:09 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent 
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/rosemary/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.] 
Jun 20, 2017 12:29:10 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["http-nio-8080"] 
Jun 20, 2017 12:29:10 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 
INFO: Using a shared selector for servlet write/read 
Jun 20, 2017 12:29:10 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["ajp-nio-8009"] 
Jun 20, 2017 12:29:10 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 
INFO: Using a shared selector for servlet write/read 
Jun 20, 2017 12:29:10 PM org.apache.catalina.startup.Catalina load 
INFO: Initialization processed in 1553 ms 
Jun 20, 2017 12:29:10 PM org.apache.catalina.core.StandardService startInternal 
INFO: Starting service [Catalina] 
Jun 20, 2017 12:29:10 PM org.apache.catalina.core.StandardEngine startInternal 
INFO: Starting Servlet Engine: Apache Tomcat/9.0.0.M21 
Jun 20, 2017 12:29:11 PM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Jun 20, 2017 12:29:11 PM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Jun 20, 2017 12:29:11 PM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Jun 20, 2017 12:29:11 PM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
Jun 20, 2017 12:29:11 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-nio-8080"] 
Jun 20, 2017 12:29:11 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["ajp-nio-8009"] 
Jun 20, 2017 12:29:11 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 1488 ms 
Tue Jun 20 12:29:18 BST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 
Tue Jun 20 12:29:26 BST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 

這裏沒有打印出任何例外。它導致超時。評分的MySQL值是int,反饋是varchar。

+1

你是如何搭擋看起來像? – Jens

+0

爲什麼你總是更新最後插入的記錄?如果你有多個用戶,你永遠不會知道這個記錄會被更新 – Jens

+0

這是一個小型的內部項目,所以目前只有一個用戶會一次使用。 – Rosemary

回答

0

由於某種原因,LAST_INSERT_ID()返回0,所以SQL查詢從未起作用,這就是它從未進入異常的原因。

0

您不應該使用JOptionPane向用戶提供反饋。 JOptionPane(Swing框架)適用於獨立客戶端。您正在編寫servlet,因此客戶端是瀏覽器。你應該使用response.getWriter()來獲得一個writer,然後使用writer.println(「<html>」); ... writer.println(「< <html>」);做你的輸出。

也許JOptionPane導致錯誤(檢查您的服務器日誌),因此SQL更新語句沒有提交。所以對數據庫沒有影響。