ExecuteUpdate java SQL expression not working

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

My problem occurs when I try to execute an update or insert statement where nothing happens. Here is my code:

The method the problem is in

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 Features

 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 a picture of my very simple database: enter image description here

The result in the console is as follows, although there were no changes to the database:

Database opened successfully

1

Database closed successfully

Thanks in advance!

+5
source share
1 answer

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

or

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

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

+7
source

Source: https://habr.com/ru/post/1258905/


All Articles