You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse接收HTML表单值插入MySQL表报SQL语法错误求助

SQL插入报错排查与修复

核心错误根因

报错堆栈提示语法错误出现在'logs' values位置,本质是实际执行的SQL语句用单引号包裹了表名logs:

  • MariaDB/MySQL中,单引号是字符串值的边界符,仅能用来包裹文本参数,不能包裹表名、列名这类标识符
  • 标识符如需包裹,只能使用反引号(`),非保留字的标识符可以直接不加任何引号
    你代码中写的插入SQL本身反引号的写法是正确的,出现单引号大概率是之前调试时修改SQL残留、或者代码编译未生效,清理重新编译后确保插入SQL为如下写法即可:
INSERT INTO logs(uuid, title, content, createTimestamp) VALUES (?,?,?,?)

其他关联问题修复

  • 代码结构错误:你粘贴的代码中LogsServlet的doPost方法未正确闭合,把DBConnection、ApplicationDao的类定义嵌到了Servlet方法内部,需要把每个公共类拆分到独立的.java文件中,保证方法、类的大括号成对闭合
  • 逻辑顺序颠倒:当前代码先向前端返回“创建成功/失败”的响应,再执行数据库插入操作,需要调整为先执行数据库操作,根据执行结果再返回对应提示
  • 字段类型不匹配:建表语句中createTimestamp为Date类型,但你传入的是yyyy.MM.dd.HH.mm.ss格式的字符串,不符合数据库日期解析规则,二选一修复即可:
    • 方案1:修改表结构,将createTimestamp改为CHAR(20)类型,直接存储格式化后的时间字符串
    • 方案2:改用标准时间格式yyyy-MM-dd HH:mm:ss,或直接传入java.sql.Timestamp类型参数,示例:
      statement.setTimestamp(4, new java.sql.Timestamp(System.currentTimeMillis()));
      
  • JDBC配置问题:从报错堆栈看你用的是8.0+版本的MySQL驱动,需要把驱动类名改为com.mysql.cj.jdbc.Driver,连接URL补充时区与编码参数:
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/loggy?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai",dbUser,dbPassword);
    
  • 资源泄漏问题:JDBC连接、Statement对象用完必须关闭,推荐用try-with-resources语法自动管理资源,避免连接耗尽
  • 无效代码清理:doPost方法中读取了表单不存在的description参数,直接删除即可
  • 乱码问题:所有请求、响应提前设置UTF-8编码,避免中文乱码:
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    
日志列表展示功能实现
  1. 复用现有ApplicationDao的listLogs()方法查询全量日志数据
  2. 调整LogsServlet的doGet方法,在新建日志表单下方追加日志列表渲染逻辑,用HTML表格展示每条日志的标题、内容、创建时间
  3. 增加HTML特殊字符转义逻辑,避免日志内容包含HTML标签触发XSS注入
    核心代码示例:
// 简单HTML转义工具方法
private String escapeHtml(String input) {
    if (input == null) return "";
    return input.replace("&", "&")
            .replace("<", "&lt;")
            .replace(">", "&gt;")
            .replace("\"", "&quot;")
            .replace("'", "&#39;");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    ApplicationDao dao = new ApplicationDao();
    List<Log> logs = dao.listLogs();

    StringBuilder html = new StringBuilder();
    html.append("<html><head><link rel=\"stylesheet\" href=\"style.css\"><title>Loggy</title></head><body>");
    // 新建日志表单
    html.append("<form id=\"logForm\" action=\"LogsServlet\" method=\"post\">")
            .append("<h3>Create A New Log!</h3>")
            .append("<label for=\"title\">Title :</label><br>")
            .append("<input type=\"text\" name=\"title\" maxLength=\"60\"><br>")
            .append("<label for=\"content\">Content :</label><br>")
            .append("<input type=\"text\" name=\"content\"><br>")
            .append("<button type=\"submit\">Submit Log</button>")
            .append("</form>");
    // 日志列表
    html.append("<h3>Log List</h3>")
            .append("<table border=\"1\" cellpadding=\"5\"><tr><th>Title</th><th>Content</th><th>Create Time</th><th>Operation</th></tr>");
    for (Log log : logs) {
        html.append("<tr>")
                .append("<td>").append(escapeHtml(log.getTitle())).append("</td>")
                .append("<td>").append(escapeHtml(log.getContent())).append("</td>")
                .append("<td>").append(escapeHtml(log.getCreateTimestamp())).append("</td>")
                .append("<td>")
                // 删除按钮表单
                .append("<form action=\"LogsServlet\" method=\"post\" style=\"display:inline\">")
                .append("<input type=\"hidden\" name=\"action\" value=\"delete\">")
                .append("<input type=\"hidden\" name=\"uuid\" value=\"").append(log.getUUID()).append("\">")
                .append("<button type=\"submit\">Delete</button>")
                .append("</form></td>")
                .append("</tr>");
    }
    html.append("</table></body></html>");
    response.getWriter().write(html.toString());
}
单条日志删除功能实现
  1. 在ApplicationDao中新增按UUID删除日志的方法:
public boolean deleteLog(String uuid) {
    String deleteSql = "DELETE FROM logs WHERE uuid = ?";
    try (Connection conn = DBConnection.getConnectionToDatabase();
         PreparedStatement stmt = conn.prepareStatement(deleteSql)) {
        stmt.setString(1, uuid);
        return stmt.executeUpdate() > 0;
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }
}
  1. 调整LogsServlet的doPost方法,通过请求参数区分操作类型:如果是删除请求就调用删除方法,执行完成后重定向回列表页,避免表单重复提交;如果是新建请求就走插入逻辑。
    核心代码示例:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String action = request.getParameter("action");
    ApplicationDao dao = new ApplicationDao();
    // 处理删除请求
    if ("delete".equals(action)) {
        String uuid = request.getParameter("uuid");
        dao.deleteLog(uuid);
        response.sendRedirect("LogsServlet");
        return;
    }
    // 处理新建日志请求
    String title = request.getParameter("title");
    String content = request.getParameter("content");
    String uuid = UUID.randomUUID().toString();
    String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());

    Log log = new TextLog();
    log.setTitle(title);
    log.setContent(content);
    log.setUUID(uuid);
    log.setCreateTimestamp(createTime);

    String insertSql = "INSERT INTO logs(uuid, title, content, createTimestamp) VALUES (?,?,?,?)";
    try (Connection conn = DBConnection.getConnectionToDatabase();
         PreparedStatement stmt = conn.prepareStatement(insertSql)) {
        stmt.setString(1, log.getUUID());
        stmt.setString(2, log.getTitle());
        stmt.setString(3, log.getContent());
        stmt.setString(4, log.getCreateTimestamp());
        stmt.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    // 新建完成重定向回列表页
    response.sendRedirect("LogsServlet");
}

内容的提问来源于stack exchange,提问作者Caitlyn Gibbons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 01:57:23