首页 文章

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

提问于
浏览
0

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

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;查看与您的MySQL服务器版本对应的手册,以便在第1行的'WHERE cus_id ='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);
     }
   }
}

4 回答

  • 0

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

    + "cus_notes = ?, "
    

    所以用 . 替换

    + "cus_notes = ? "
    
  • 0

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

  • 0

    它是控制台日志中显示的语法错误 .

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

    "cus_notes = ?"

  • -4

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

相关问题