错误:DB错误,无法执行更新查询...语法错误:第1行第92列遇到“ S”

时间:2019-12-03 12:10:00

标签: java sql-injection

我在插入新客户和更新旧客户方面遇到问题。两种方法都出现相同的错误。错误:DB错误,无法执行更新查询...语法错误:在第1行第92列遇到“ S”。我认为这与我的插入和更新语句的格式有关,但无法确定如何解决此问题。

public class Customer {

    private String name;
    private String address;
    private String state; 
    private double customernumber;


    public static final DBConnector db = new DBConnector(); 

    public static Customer[] getAllCustomers (){

        Customer[] customer = new Customer[0] ; 
        try{

        String query = "SELECT customernumber, name, address, state FROM Customer"; 
        DBConnector.openConnection();
        //execute query                
        ResultSet resultSet = DBConnector.executeReadQuery(query); 

       // Get the number of rows.
         resultSet.last();                 // Move to last row
         int numRows = resultSet.getRow(); // Get row number
         customer  = new   Customer[numRows];
         resultSet.first();                // Move to first row

         //create list of Custoemrs
         for(int i = 0; i<numRows; i++){
             String name = resultSet.getString(1);
             String address = resultSet.getString(2);
             String state = resultSet.getString(3);
             double customernumber = resultSet.getDouble(4);
             customer[i] = new Customer(name,address,state,customernumber);
             resultSet.next(); 
         }

         //close conneciton   
       DBConnector.closeConnection();
          }
       catch(SQLException e)
       {
           System.out.println ("SQL Exception in getAllCustomers " + e.getMessage());
       }    
       return customer;       
    }

    public Customer (String name, String address, String state, double customernumber){
        this.name = name; 
        this.address = address; 
        this.state = state;   
        this.customernumber=customernumber;

    }



    public Customer (CustomerGUI customerGUI){
        this.name = customerGUI.getCustName(); 
        this.address = customerGUI.getCustAddress(); 
        this.state = customerGUI.getCustState(); 
        this.customernumber = customerGUI.getCustID();
    }

    public String getName(){
        return name;
    }

    public String getAddress(){
        return address;
    }

    public String getState(){
        return state; 
    }

   public double getID(){
        return customernumber;
    }

    public void setAddress (String address){
        this.address = address; 
    }

    public void setState (String state){
        this.state = state; 
    }
    public void setID(double customernumber){
        this.customernumber=customernumber;
    }

    public void setName(String name){
        this.name=name;
    }



    public boolean addCustomer() {
      boolean result = true;   
        if(exists()) {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
             alert.setTitle("ERROR");
             alert.setHeaderText("Duplicate Customer Name");
             alert.setContentText("This Customer already exists in the Customer list");
             alert.show();
             result = false; 
        }
        else{

            String query  = "INSERT INTO Customer (Name, Address, State, CustomerNumber) VALUES ('" + name + "', " + address + ", '" + state + ", " 
                    + customernumber +  " )";
            DBConnector.openConnection();
            if (DBConnector.executeUpdateQuery(query)<=0) 
               result = false ;
            DBConnector.closeConnection();

        }
        return result;    
    }

    public boolean updateCustomer(){
      boolean result = true;   
       String query = "UPDATE Customer SET  Name = '" + name + "' Address='" + address + "', State = " + state + " WHERE  CustomerNumber =  '" + customernumber + "'";
       DBConnector.openConnection();
       if (DBConnector.executeUpdateQuery(query)<=0) 
               result = false ;
        DBConnector.closeConnection();
        return result;  
    }


    public boolean deleteCustomer (){
           boolean result = true;  
           if(exists()){
           String query = "DELETE FROM Customer WHERE Name = '" +  this.name + "'"  ;  
           DBConnector.openConnection();
           if (DBConnector.executeUpdateQuery(query)<=0) 
               result = false ;
           DBConnector.closeConnection();

        }
            return result; 
    }

    private boolean exists()   
    {
       boolean custExists = false; 
       try{
       String query = "SELECT name, address, state FROM Customer WHERE Name = '" +  this.name + "'"  ; 
       DBConnector.openConnection();
       ResultSet rs = DBConnector.executeReadQuery(query);
       if (rs.next()){
           custExists = true;
       }             
        DBConnector.closeConnection();
       }catch (SQLException e){
           custExists = false;
       }
       return custExists;

}

}





//Demo Class
import coffee.business.Customer;

public class DemoCustomer {


    public static void main(String[] args) {


       Customer customer2 = new Customer("Primary Grocery","410 S Main","MI",102);
        System.out.println("Customer #2 " + customer2.addCustomer());



    }

}

0 个答案:

没有答案