2017-04-24 68 views
0

我在嘗試在tomcat上運行java文件時遇到問題。我想可以編輯我放入數據庫的記錄,當我編輯記錄時他們什麼也不做。當我嘗試編輯記錄的Tomcat拋出我這個錯誤你的SQL語法有錯誤;檢查對應於你的MySQL服務器的手冊

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); 
    } 
    } 
} 
+0

您在這裏有太多的代碼更新詢問 –

+1

錯字:'+「cus_notes = ?,「'(在WHERE之前的額外逗號)。 –

回答

0

你有一個逗號,上的最後一個值

+ "cus_notes = ?, " 
+ "cus_notes = ? " 
0

行,以便更換:從+ "cus_notes = ?, "刪除逗號。

+0

答案已更正。 – toonice

-4

你應該發佈更多的錯誤,它更容易識別錯誤,現在根據你的提示是SQL語法錯誤。

+0

錯誤消息是不言自明的 – Strawberry

0

它是語法錯誤,如控制檯日誌中所示。

你終於價值到底有逗號,刪除它,並給一個嘗試

"cus_notes = ?"

相關問題