Search code examples
javasqlderby

updating records in derby database not working


Can someone help me, the problem is when I run this code I keep getting this Syntax error: Encountered "[" at line 1, column 24 . BUILD SUCCESSFUL (total time: 44 seconds) I can't see where I made a mistake in the code

String id = JOptionPane.showInputDialog(null, "Please enter the Employess ID", JOptionPane.QUESTION_MESSAGE);

                String fname = JOptionPane.showInputDialog(null, "Please enter Employess First NAME", JOptionPane.QUESTION_MESSAGE);
                String sname = JOptionPane.showInputDialog(null, "Please enter Employess Surname NAME", JOptionPane.QUESTION_MESSAGE);
                String pss = JOptionPane.showInputDialog(null, "Please enter the Employess PPS Number", JOptionPane.QUESTION_MESSAGE);   
                String cnum = JOptionPane.showInputDialog(null, "Please ente thr Employess Contact Number", JOptionPane.QUESTION_MESSAGE);
                String address  = JOptionPane.showInputDialog(null, "Please enter Employess  Address", JOptionPane.QUESTION_MESSAGE);
                String dep = JOptionPane.showInputDialog(null, "Please enter Employess Deparment ", JOptionPane.QUESTION_MESSAGE);
                String sal = JOptionPane.showInputDialog(null, "Please enter Employess  salary", JOptionPane.QUESTION_MESSAGE);

            JOptionPane.showConfirmDialog(null, id + sname +  "Records have been entered ");

       try{

    String host = "jdbc:derby://localhost/ee ";
    String uName = "aa";
    String uPass = "bb";
Connection con = DriverManager.getConnection(host, uName, uPass);
//Connection con = DriverManager.getConnection(host);
//Customer Lookup by Last Name

//SQL

String sql = "UPDATE emp SET  fname = [?],sname = [?],pss = [?],cnum = [?],address = [?],dep = [?],sal =[?] where id = [?]";



prest = con.prepareStatement(sql);


    prest.setString(1,id);
    prest.setString(2,fname);
    prest.setString(3,sname);
    prest.setString(4,pss);
    prest.setString(5,cnum);
    prest.setString(6,address);
    prest.setString(7,dep);
    prest.setString(8,sal);
    prest.executeUpdate();


    }
    catch (SQLException err) {

    System.out.println(err.getMessage());
    }
    }
                }

    }    

Solution

  • A parameter placeholder (?) should always immediately follow the equal sign without delimiters of any kind:

    String sql = "UPDATE emp SET fname=?, sname=?, pss=?, cnum=?, address=?, dep=?, sal=? WHERE id=?";
    

    or

    String sql = "UPDATE emp SET fname = ?, sname = ?, pss = ?, cnum = ?, address = ?, dep = ?, sal = ? WHERE id = ?";