【发布时间】:2017-11-22 16:03:32
【问题描述】:
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 查询中的问题吗?谢谢!
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。
【问题讨论】:
-
你的 catch-block 长什么样子?
-
为什么总是更新最后插入的记录?如果您有多个用户,您永远不知道哪条记录会被更新
-
这是一个小型的内部项目,所以暂时只有一个用户会使用一次。
-
你看到消息
Successfully inserted了吗?如果你得到一个,你永远不会打印出异常。如果遇到异常,请使用日志记录框架并注销消息 -
然后你得到一个不会显示的异常,因为你的代码捕获了异常并且没有以任何方式显示它
标签: java mysql servlets prepared-statement