【发布时间】: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