-2

//in context listener

Statement stmt;
try {
  Class.forName("oracle.jdbc.OracleDriver");
  Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","abc", "abc");
  stmt = conn.createStatement();
  sce.getServletContext().setAttribute("stmt", stmt);
} catch (ClassNotFoundException e) {
} catch (SQLException e) {
}

//in Servlet page

try {
  Statement stmts=(Statement) getServletContext().getAttribute("stmt");
  stmts.executeUpdate("INSERT INTO COMPANYS values(1,'ahmed',5,'t012','t345','email@eamil','adressadress')");
  System.out.println("connection succeed");
} catch (SQLException e) {
  System.out.println("connection fail");
}

in Servlet page "try code" is execute and "connection succeed" is appearing but in oracle database there is no data inserted >> why???

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131

2 Answers2

0

use con.setAutoCommit(false); after conn has created. Once the transaction completes i.e.,after executeUpdate(...) call con.commit(); It will solve your issue if their are no errors displayed on the console.

participantjava
  • 195
  • 1
  • 2
  • 16
0

Try it again with below steps

  • Set auto commit as false
  • Commit after updating the record to make it persisted in the database
  • Rollback if there is any exception while inserting the record
  • Don't forget to clean-up the environment such as ResultSet, Statement etc.
  • Code in finally block to close the resources
  • Don't keep connection opened for long time.

Must read

Find a sample code HERE with detailed inline comments.

Braj
  • 46,415
  • 5
  • 60
  • 76