1

I am having problems inserting data into a postgres table created using java. The created table part of the code works fine, its only when I am inserting values into the table that nothing happens. The code I am using to populate the table tt is:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      String txt1 = jTextField1.getText();
      int number = Integer.parseInt(txt1);
      String name= jTextField2.getText(); 
      String txt3 = jTextField3.getText();
      int mean = Integer.parseInt(txt3);
      Connection c = null;
      Statement stmt = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/tst", "postgres", "21262050");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");
         String sql  = "INSERT INTO tt(noun, max, nbr) VALUES(?, ?, ?)";
         PreparedStatement pst =  c.prepareStatement(sql);
         pst.setString(1, name);
         pst.setInt(2,mean );
         pst.setInt(3, number);

         pst.executeUpdate();

      } catch (SQLException ex) {
            Logger.getLogger(insrt.class.getName()).log(Level.SEVERE, null, ex);
      }


      catch(Exception e){
            e.printStackTrace();
        }

    }                                        

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new insrt().setVisible(true);
            }
        });
    }
0

1 Answer 1

3

You're establishing autocommit to false.

After the auto-commit mode is disabled, no SQL statements are committed until you call the method commit explicitly.

https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html#disable_auto_commit

Try executing c.commit() after pst.executeUpdate();

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

2 Comments

I would question why it is necessary to turn off auto commit for this example?
@TheShady No problem. Mark the answer as accepted if you please.

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.