Reputation: 13
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);
}
});
}
Upvotes: 1
Views: 970
Reputation: 2468
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();
Upvotes: 3