【问题标题】:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server您的 SQL 语法有错误;检查与您的 MySQL 服务器相对应的手册
【发布时间】:2017-09-20 15:48:04
【问题描述】:

尝试在 tomcat 上运行 java 文件时遇到问题。我想能够编辑我放入数据库的记录,当我编辑记录时他们什么都不做。当我尝试编辑记录时,Tomcat 会抛出此错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在 'WHERE cus_id = 附近使用正确的语法 第 1 行的“13”

我认为这是有错误的代码,因为它包含所有 SQL 命令。删除功能有效。我只是无法编辑记录...

感谢您的帮助!

package crud.data;

import java.sql.*;
import java.util.ArrayList;

import crud.business.Customer;

public class CustomerDB
{
 //insert method (pass customer object to parameter customer)
 public static int insert(Customer customer)
{
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
//comment
String query
  = "INSERT INTO customer (cus_fname, cus_lname, cus_street, cus_city, 
   cus_state, cus_zip, cus_phone, cus_email, cus_balance, cus_total_sales, 
   cus_notes) "
    + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try {
  ps = connection.prepareStatement(query);
  ps.setString(1, customer.getFname());
  ps.setString(2, customer.getLname());
  ps.setString(3, customer.getStreet());
  ps.setString(4, customer.getCity());
  ps.setString(5, customer.getState());
  ps.setString(6, customer.getZip());
  ps.setString(7, customer.getPhone());
  ps.setString(8, customer.getEmail());
  ps.setString(9, customer.getBalance());
  ps.setString(10, customer.getTotalSales());
  ps.setString(11, customer.getNotes());

  return ps.executeUpdate();
} catch (SQLException e){
  System.out.println(e);
  return 0;
} finally {
  DBUtil.closePreparedStatement(ps);
  pool.freeConnection(connection);
    }
  }

 //update method
 public static int update(Customer customer)
 {
  ConnectionPool pool = ConnectionPool.getInstance();
  Connection connection = pool.getConnection();
  PreparedStatement ps = null;

String query = "UPDATE customer SET "
    + "cus_fname = ?, "
    + "cus_lname = ?, "
    + "cus_street = ?, "
    + "cus_city = ?, "
    + "cus_state = ?, "
    + "cus_zip = ?, "
    + "cus_phone = ?, "
    + "cus_email = ?, "
    + "cus_balance = ?, "
    + "cus_total_sales = ?, "
    + "cus_notes = ?, "
    + "WHERE cus_id = ?";
try {
  ps = connection.prepareStatement(query);
  ps.setString(1, customer.getFname());
  ps.setString(2, customer.getLname());
  ps.setString(3, customer.getStreet());
  ps.setString(4, customer.getCity());
  ps.setString(5, customer.getState());
  ps.setString(6, customer.getZip());
  ps.setString(7, customer.getPhone());
  ps.setString(8, customer.getEmail());
  ps.setString(9, customer.getBalance());
  ps.setString(10, customer.getTotalSales());
  ps.setString(11, customer.getNotes());
  ps.setString(12, customer.getId());

  return ps.executeUpdate();
} catch (SQLException e) {
  System.out.println(e);
  return 0;
} finally {
  DBUtil.closePreparedStatement(ps);
  pool.freeConnection(connection);
     }
   }
}

【问题讨论】:

  • 这里的代码太多了
  • UPDATE 查询中的错字:+ "cus_notes = ?, "WHERE 之前的额外逗号)。

标签: java mysql sql tomcat syntax


【解决方案1】:

这是控制台日志中显示的语法错误。

你在最后一个值的末尾有逗号,删除它并尝试一下

"cus_notes = ?"

【讨论】:

    【解决方案2】:

    你应该贴更多的错误,更容易识别错误在哪里,现在根​​据你的提示是SQL语法错误。

    【讨论】:

      【解决方案3】:

      第 64 行:删除 + "cus_notes = ?, " 中的逗号。

      【讨论】:

        【解决方案4】:

        你在最后一个值上有一个逗号,

        + "cus_notes = ?, "
        

        所以替换为

        + "cus_notes = ? "
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多