-2

I'm trying to update or delete data from database but it's not working. I dont know I did something yesterday night while sleepy trying to fix another problem. And now this is not working. Here's the code for both of them. Where's my problem about that. When I use delete command, it's updating but trying to update table,it's not updating table. Where is my problem , How can I fix this?

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

        String sql="delete  from maintable where İsim =?   ";



        try{

        pst=conn.prepareStatement(sql);

        pst.setString(1, jTextField1.getText());

        pst.execute();

        JOptionPane.showMessageDialog(null, "Silindi");


        }catch(Exception e){

        JOptionPane.showMessageDialog(null, e);


        }

        Update_table();




    }             

This is for update:

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

try{

String value1=jTextField1.getText();
String value2=jTextField2.getText();
String value3=jTextField3.getText();
String value4=jTextField4.getText();
String value5=jTextField5.getText();



String sql="update maintable set İsim='"+value1+"' ,Yaş ='"+value2+"',Konum ='"+value3+"',EPosta='"+value4+"',KatılımTarihi='"+value5+"' where İsim='"+value1+"' ";
              pst=conn.prepareStatement(sql);
              pst.execute();
              JOptionPane.showMessageDialog(null, "Güncellendi");



}catch(Exception e){;

        JOptionPane.showMessageDialog(null, e);


}
  Update_table();




    }          

EDIT: "pst=conn.prepareStatement(sql);" it's giving me red underline errors, which is for sql word in brackets and says "cannot find symbol"

pst=conn.prepareStatement("update maintable set İsim=?,Yaş=?,Konum=?,EPosta=?,KatılımTarihi=?"); pst.setString(1,value1);
pst.setString(2,value2);
pst.setString(3,value3);
pst.setString(4,value4);
pst.setString(5,value5);

pst=conn.prepareStatement(sql);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Qce
  • 17
  • 1
  • 8

2 Answers2

0
private void cmd_deleteActionPerformed(java.awt.event.ActionEvent evt) {                                           

    String sql="delete  from maintable where İsim =?   ";



    try{

    pst=conn.prepareStatement(sql);

    pst.setString(1, jTextField1.getText());

    pst.executeUpdate();// **error was here**

    JOptionPane.showMessageDialog(null, "Silindi");


    }catch(Exception e){

    JOptionPane.showMessageDialog(null, e);


    }

    Update_table();




}  

this is for update

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

try{

String value1=jTextField1.getText();
String value2=jTextField2.getText();
String value3=jTextField3.getText();
String value4=jTextField4.getText();
String value5=jTextField5.getText();



String sql="update maintable set İsim='"+value1+"' ,Yaş ='"+value2+"',Konum ='"+value3+"',EPosta='"+value4+"',KatılımTarihi='"+value5+"' where İsim='"+value1+"' ";
              pst=conn.prepareStatement(sql);
              pst.executeUpdate();
              JOptionPane.showMessageDialog(null, "Güncellendi");



}catch(Exception e){;

        JOptionPane.showMessageDialog(null, e);


}
  Update_table();




    }          

For inserting,updating deleting you should call executeUpdate() not execute()

Note This is not the proper way of using preparedStatement

 String sql="update maintable set İsim='"+value1+"' ,Yaş ='"+value2+"',Konum ='"+value3+"',EPosta='"+value4+"',KatılımTarihi='"+value5+"' where İsim='"+value1+"' ";
                  pst=conn.prepareStatement(sql);

Use this way

 pst=conn.prepareStatement("update maintable set İsim=?,Yaş=?,Konum=?,EPosta=?,KatılımTarihi=?"); pst.setString(1,value1);
pst.setString(2,value2);
pst.setString(3,value3);
pst.setString(4,value4);
pst.setString(5,value5);

pst.executeUpdate();
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Hmm, thanks for answering but it didnt work. Both of them agains same ; just deleting on jtable or just updating jtable, not database. So when i re-log to program datas not staying same which i edited or deleted. And i tried your last advice which you gave me for using that but, when i apply this code to my program, it's giving some red underline error . I'll edit my question. – Qce Aug 29 '13 at 09:36
  • @Qce I can not say why you are getting error but this is the proper use of preparedstatement.You check it here http://stackoverflow.com/questions/5591271/how-to-use-prepared-statement and here http://www.roseindia.net/jdbc/jdbc-mysql/TwicePreparedStatement.shtml – SpringLearner Aug 29 '13 at 09:42
  • @ace I have edited my answer,just copy and paste in your code,If you find errors then you can ask me – SpringLearner Aug 29 '13 at 09:45
  • It's not giving error as code but, it didnt work. Same thing, shows my edit on programram, when i re-run program it's all gone. PS:I'm using SQLite Manager for database – Qce Aug 29 '13 at 09:52
  • @qce what you wanted to do and what you get where its not working everything details you need to post.Unless we say the codes we can not solve.I am just telling the proper use of preparedstatement and where to use executeUpdate().This is in accordance with the question you have posted – SpringLearner Aug 29 '13 at 09:56
  • I wanted to delete from database or update database , not just jtable for temp. But it's just updating or deleting from table and it does not work for database. I just tried to open multiple jframes. On that it's not working too . Here's my other topic, i just removed frames from project for now and clean codes for them. But maybe you can find solution for looking them. http://stackoverflow.com/questions/18499736/database-locked-while-trying-to-open-multiple-jframes – Qce Aug 29 '13 at 10:03
  • @qce I can only tell if there is any error in JDBC part because i do not know swings.The code i have posted is 100% correct and it will work.If you say its not working then there is some other problem in your code but JDBC codes are 100% correct – SpringLearner Aug 29 '13 at 10:06
  • Thanks anyway man, if i can solve this problem today. ı can accept your answer :) – Qce Aug 29 '13 at 10:12
0

Instead of pst.execute() use pst.executeUpdate() then it will work fine.

For transactions(INSERT, DELETE, UPDATE) use the method executeUpdate().

For selecting(SELECT) data use executeQuery().

  • String sql="update maintable set İsim='"+value1+"' ,Yaş ='"+value2+"',Konum ='"+value3+"',EPosta='"+value4+"',KatılımTarihi='"+value5+"' where İsim='"+value1+"' "; pst=conn.prepareStatement(sql); pst.executeUpdate(); JOptionPane.showMessageDialog(null, "Güncellendi"); That didnt work for both of them – Qce Aug 29 '13 at 09:59