Ich habe Probleme mit einer Java-Datei, wenn ich versuche, sie mit Tomcat auszuführen. Ich bin in der Lage, Datensätze zu bearbeiten, die ich in eine Datenbank einlege, wenn ich die Datensätze bearbeite, tun sie nichts. Wenn ich versuche, um die Datensätze zu bearbeiten Tomcat mir diesen FehlerSie haben einen Fehler in Ihrer SQL-Syntax; Überprüfen Sie das Handbuch, das Ihrem MySQL-Server entspricht.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException wirft: Sie haben einen Fehler in der SQL-Syntax; Sie in der Bedienungsanleitung für die richtige Syntax zu verwenden in der Nähe von ‚WHERE cus_id = ‚13‘‘ in Zeile 1
ich meine, das ist der Code, der den Fehler hat, weil sie alle auf Ihre MySQL-Server-Version entspricht hat der SQL-Befehle. Die Löschfunktion funktioniert. Ich kann einen Datensatz einfach nicht bearbeiten ...
Danke für die Hilfe!
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);
}
}
}
Sie haben viel zu viel Code hier –
Typo in UPDATE-Abfrage: '+„cus_notes = ?, "' (zusätzliches Komma vor 'WHERE'). –