7

I am learning how to use SQL with Java. I have installed the JDBC driver successfully and I am able to read the records from a database and print it on the screen.

My problem occurs when trying to do either an update or insert statement, where nothing happens. Here is my code:

Method where the problem resides

public static void updateSchools(ArrayList<String> newSchool)
{
    try
    {
        openDatabase();
        stmt = c.createStatement();
        int numberOfRows = stmt.executeUpdate("UPDATE schools SET address='abc' WHERE abbreviation='2';");
        System.out.println(numberOfRows);
        closeDatabase();
    }
    catch (Exception e)
    {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
}

Support functions

public static void openDatabase()
{
    c = null;
    stmt = null;
    try
    {
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Badminton", "postgres", "postgrespass");
        c.setAutoCommit(false);
    }
    catch (Exception e)
    {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    System.out.println("Database opened successfully");
}

public static void closeDatabase()
{
    try
    {
        stmt.close();
        c.close();
    }
    catch (Exception e)
    {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    System.out.println("Database closed successfully");
}

Here is an image of my very simple database: enter image description here

The result in the console is the following, although no databases changes were done:

Database opened successfully

1

Database closed successfully

Thanks in advance!

5
  • don't use ";" it can breaks the sql; are you getting any exception ? Commented Oct 27, 2016 at 16:40
  • Mybe you need to refresh your db, or commit c.setAutoCommit(True); Commented Oct 27, 2016 at 16:42
  • @karelss From the Postgres documentation: Get in the habit of including those SQL semicolons Commented Oct 27, 2016 at 16:43
  • Learn "try with resources". Commented Oct 27, 2016 at 16:56
  • try(Connection con = getConnection(url, username, password, "org.postgresql.Driver"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql); ) { //statements }catch(....){} Read more here stackoverflow.com/questions/2225221/… Commented Oct 27, 2016 at 16:58

2 Answers 2

11

Remove c.setAutoCommit(false) line from the openDatabase method.

Or

Add c.commit() at the end of the updateSchool method.

After the auto-commit mode is disabled, no SQL statements are committed until you call the method commit explicitly. All statements executed after the previous call to the method commit are included in the current transaction and committed together as a unit.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, my code now works as intended :).
An explanation why this solves the problem would add just that little extra to your answer.
My insert and update queries were not committed to the database, which is why I would not seen any changes. Once I added the commit line after my operations, the changes were done successfully.
1

The OP's query has been solved, but this might help someone.

It turns out that you cannot execute two different Statements(or PreparedStatements) in a single batch.

I was having the same problem, no errors, no exceptions, the database record simply wouldn't update as it should.

Resusing the previously used PreparedStatement solved the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.