Question

I want to update some data in a database, and I keep on getting the following error: Java.sql.SQLException: unrecognized tokens:"Hello"

My question is, how do I solve the issue I'm having?

You can find my code below:

 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

   String  value1= jTextField1.getText();
    try{

     String sql = "Update KKKB1 set Pangkat='"+value1+"' where Pangkat='"+value1+"  ";
     pst=conn.prepareStatement(sql);

     pst.executeUpdate();
     JOptionPane.showMessageDialog(null,"Updated");
     Main ad=new Main();
     ad.setVisible (true);
     dispose();

   }

No correct solution

OTHER TIPS

PreparedStatement escapes String and Date variables for you.

 String sql = "Update KKKB1 set Pangkat= ? where Pangkat=? ";
 pst=conn.prepareStatement(sql);
 pst.setString(1, p1);
 pst.setString(2, p2);

http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

I think this code is a very bad idea. You should be closing your Connection & PreparedStatement in method scope. You should not be mingling UI and persistence code this way. You don't commit or rollback if the INSERT fails.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top