您的SQL语法有错误;查看与MySQL服务器对应的手册

时间:2017-04-24 04:50:45

标签: java mysql sql tomcat syntax

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

  

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

我认为这是具有错误的代码,因为它具有所有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);
     }
   }
}

4 个答案:

答案 0 :(得分:0)

最后一个值

上有一个逗号,
+ "cus_notes = ?, "

所以用

替换
+ "cus_notes = ? "

答案 1 :(得分:0)

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

答案 2 :(得分:0)

这是语法错误,如控制台日志中所示。

  

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

"cus_notes = ?"

答案 3 :(得分:-4)

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