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: 
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!
source share