Results 1 to 6 of 6
  1. #1
    OMFGITSROHIT is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default Update statement not working.

    When I run the program, there seems to be a problem in the

    PreparedStatement s2=con.prepareStatement(query+" where "+condition);
    s2.execute();

    part. Until then everything seems fine. The System.out.println(query+" where "+condition); prints just what I want it to print. Only, it doesn't seem to be getting executed. It just gets stuck and I get no error. I have to stop it manually outside the window. The code in red is the part where I'm having trouble.

    Java Code:
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String option,query="",condition="";
    if(!jTextField2.getText().contentEquals(""))
    {
        if(!jRadioButton1.isSelected())
        {
     if(condition.isEmpty())
     {
         condition="name='"+jTextField2.getText()+"'";
     }
     else
     {
    condition+=" and name='"+jTextField2.getText()+"'";    
    }
    }
    }
    if(!jTextField3.getText().contentEquals(""))
    {
        if(!jRadioButton2.isSelected())
        {
     if(condition.isEmpty())
     {
         condition="subjects_handled='"+jTextField3.getText()+"'";
     }
     else
     {
    condition+=" and subjects_handled='"+jTextField3.getText()+"'";    
    }
    }
    }
    if(!jTextField4.getText().contentEquals(""))
    {
        if(!jRadioButton3.isSelected())
        {
     if(condition.isEmpty())
     {
         condition="desig='"+jTextField4.getText()+"'";
     }
     else
     {
    condition+=" and desig='"+jTextField4.getText()+"'";    
    }
    }
    }
    if(!jTextField5.getText().contentEquals(""))
    {
        if(!jRadioButton4.isSelected())
        {
     if(condition.isEmpty())
     {
         condition="idno='"+jTextField5.getText()+"'";
     }
     else
     {
    condition+=" and idno='"+jTextField5.getText()+"'";    
    }
    }
    }
    if(jTextField2.getText().equals(""))
    {
        if(jTextField3.getText().equals(""))
        {
            if(jTextField4.getText().equals(""))
            {
                if(jTextField5.getText().equals(""))
                {
                    JOptionPane.showMessageDialog(rootPane,"Enter values");
                }
            }
        }
    }
           
            if (jRadioButton1.isSelected())
            {
               option="name";
               query="update "+Exam_Season.dept2+" set name='"+jTextField2.getText()+"'";
                       }
    if (jRadioButton2.isSelected())
            {
               option="subjects";
               query="update "+Exam_Season.dept2+" set subjects_handled='"+jTextField3.getText()+"'";
            }
    if (jRadioButton3.isSelected())
            {
               option="desig";
               query="update "+Exam_Season.dept2+" set desig='"+jTextField4.getText()+"'";
            }
    if (jRadioButton4.isSelected())
            {
               option="idno";
               query="update "+Exam_Season.dept2+" set idno='"+jTextField5.getText()+"'";
            }
    [B][COLOR="Red"]Connection con = null;
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String user = "scott";
    String pass = "tiger";
    try{
    System.out.println(query+ " where "+condition);
    Class.forName(driver);
    con = DriverManager.getConnection("jdbc:odbc:login",user,pass);
    Statement s=con.createStatement();
    //System.out.println("so far so good");
    //s.execute(query+" where "+condition);
    PreparedStatement s2=con.prepareStatement(query+" where "+condition);
    s2.execute();
    JOptionPane.showMessageDialog(rootPane, "Successfully updated");
    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(rootPane, "Error.");
    e.printStackTrace();
    }
    }[/COLOR][/B]
    Last edited by OMFGITSROHIT; 04-08-2010 at 11:19 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) Use code tags when posting code.
    2.) When using a PreparedStatement, use it with parameters.
    3.) Did you check your console for any possible errors?
    4.) When running an update statement, use the executeUpdate method and check its return value.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    Hi,
    It's highly impossible to scan your code.Please put the code with code tag.
    You are telling that s.o.p is printing ur query and not executing.Best thing is directly take the printed query and execute in sql editor.Might be spaces or case problem will be there in the executing query.

    Try to put proper indentation.
    Ramya:cool:

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default

    You're doing too much in one place.
    Mixing display logic (all those option panes) in with your db logic is simply confusing things.

    Strip out the display stuff and simply have a db layer that takes the input values and builds a quiery from that.

    As has been stated, there's no point using a PreparedStatement if you're not going to set the parameters (ie using '?' and all that stuff). May as well stick to a Statement. However, since this involves user input, it should be a PreparedStatement with properly bound values.

  5. #5
    OMFGITSROHIT is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    The part in red is the only part that matters. The other part generates the value for the strings query and condition.

    System.out.println(query+ " where "+condition); prints just the statement I need to execute. Infact I type the exact words and execute it in sql.

    Whereas, in netbeans it doesn't seem to work. Execution occurs until these 3 lines of code. Here, it stops executing or maybe just keeps doing so forever. It doesn't even go to the catch part of the exception.

    Java Code:
    PreparedStatement s2=con.prepareStatement(query+" where "+condition);
    s2.execute();
    JOptionPane.showMessageDialog(rootPane, "Successfully updated");

  6. #6
    OMFGITSROHIT is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    Guys, thanks for the help. Its working now. Replaced a small part of the code and it worked.

    Java Code:
    Connection con = null;
    String driver="sun.jdbc.odbc.JdbcOdbcDriver";
    String user = "scott";
    String pass = "tiger";
    try{
    String qry=query+" where "+condition;
    System.out.println(qry);
    Class.forName(driver);
    con = DriverManager.getConnection("jdbc:odbc:login",user,pass);
    Statement s=con.createStatement();
    System.out.println("so far so good");
    s.execute(qry);
    //PreparedStatement s2=con.prepareStatement(qry);
    //s2.execute();
    JOptionPane.showMessageDialog(rootPane, "Successfully updated");
    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(rootPane, "Error.");
    e.printStackTrace();
    }

Similar Threads

  1. phonebook update
    By nanna in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 10:13 PM
  2. How to update my jdk???
    By low224 in forum New To Java
    Replies: 4
    Last Post: 01-04-2009, 04:51 PM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •