Java JDBC Tutorials
JDBC tutorial with full example, including CRUD sql statement with JDBC Statement and PreparedStatement, interact Stored Procedure with CallableStatement, JDBC transaction and how to integrate with Spring and JSF 2.0
JDBC tutorial with full example, including CRUD sql statement with JDBC Statement and PreparedStatement, interact Stored Procedure with CallableStatement, JDBC transaction and how to integrate with Spring and JSF 2.0
JDBC transaction make sure a set of SQL statements is executed as a unit, either all of the statements are executed successfully, or NONE of the statements are executed (rolled back all changes). 1. Without JDBC Transaction 1.1 A JDBC example to insert two rows and update one row. TransactionExample.java package com.mkyong.jdbc; import java.math.BigDecimal; import …
Transaction management is required to ensure the data integrity and consistency in database. Spring’s AOP technique is allow developers to manage the transaction declarative. Here’s an example to show how to manage the Hibernate transaction with Spring AOP. P.S Many Hibernate and Spring configuration files are hidden, only some important files are shown, if you …
Spring JdbcTemplate batch insert, batch update and also @Transactional examples. Technologies used : Spring Boot 2.1.2.RELEASE Spring JDBC 5.1.4.RELEASE Maven 3 Java 8 1. Batch Insert 1.1 Insert a batch of SQL Inserts together. BookRepository.java import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.BatchPreparedStatementSetter; public int[] batchInsert(List<Book> books) { return this.jdbcTemplate.batchUpdate( "insert into books (name, price) values(?,?)", new BatchPreparedStatementSetter() { …
In Hibernate, the transaction management is quite standard, just remember any exceptions thrown by Hibernate are FATAL, you have to roll back the transaction and close the current session immediately. Here’s a Hibernate transaction template : Session session = null; Transaction tx = null; try{ session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); tx.setTimeout(5); //doSomething(session); tx.commit(); }catch(RuntimeException …