Results 1 to 5 of 5
  1. #1
    Bhavana Jain is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Question Prepared statement and update Table

    Hi all,
    I am trying to update a table and it happens also but when I close the program and again when I run it the updated data is lost whether it may be inserting a new row or changing the field value. I am not getting the exact error please help me out.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Prepared statement and update Table

    show your code or small program that shows you problem

  3. #3
    Bhavana Jain is offline Member
    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    Default Re: Prepared statement and update Table

    My code is here

    import java.sql.DriverManager;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class abc {
    private Connection connection;
    private PreparedStatement updateMobileNo;
    private ResultSet rs;
    public abc()
    {
    try
    {
    Class.forName("org.hsqldb.jdbcDriver");
    connection = DriverManager.getConnection("jdbc:hsqldb:file:" +"C:\\Databasedir\\Database", "sa", "");
    updateMobileNo = connection.prepareStatement("UPDATE \"Table1\" SET \"Mobile Number\" = ? WHERE \"ID\" = ?");
    }
    catch(ClassNotFoundException exception)
    {
    exception.printStackTrace();
    }
    catch(SQLException exception)
    {
    exception.printStackTrace();
    }
    }
    public void toUpdateMobileNo(String mobileNo, int id)
    {
    try{
    updateMobileNo.setString(1, mobileNo);
    updateMobileNo.setInt(2, id);
    updateMobileNo.executeUpdate();
    }
    catch(SQLException exception)
    {
    exception.printStackTrace();
    }
    }
    public static void main(String[] args)
    {
    abc a = new abc();
    a.toUpdateMobileNo("012367", 1);

    }
    }


    I really don't understand where the problem lies because I was able to update the table with this code and I have created many rows with it but all of a sudden this problem has started, I thought problem might be with the database but I recreated the database and still the problem persist.

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Prepared statement and update Table

    please use code tags when posting code

    Not really sure about the problem but try to put setAutoCommit() to false before your PreparedStatement then commit() after the UPDATE, I assume that you know this method
    Last edited by mine0926; 08-19-2012 at 05:38 PM.

  5. #5
    zhaoyx is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Re: Prepared statement and update Table

    >updateMobileNo.executeUpdate();
    Try int count=updateMobileNo.executeUpdate();
    Then you should check whether count>0
    If count>0, you can try connection.commit();

Similar Threads

  1. SQL Prepared Statement with date
    By Dcalladi in forum New To Java
    Replies: 3
    Last Post: 02-18-2012, 03:37 PM
  2. Replies: 1
    Last Post: 01-10-2011, 01:44 AM
  3. JDBC Prepared Statement
    By Floetic in forum JDBC
    Replies: 4
    Last Post: 05-20-2009, 11:53 PM
  4. Prblem in Prepared Statement
    By haneeshrawther in forum JDBC
    Replies: 2
    Last Post: 04-25-2008, 09:49 AM
  5. Using Prepared Statement
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-06-2008, 09:22 AM

Posting Permissions

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