In this Tutorial, we will take a look at how to do Batch Update using Java JDBC PreparedStatement,
Table: studentsCREATE TABLE `students` (
`student_id` int NOT NULL AUTO_INCREMENT,
`student_name` varchar(45) NOT NULL,
`student_dob` datetime NOT NULL,
`student_address` varchar(45) NOT NULL,
PRIMARY KEY (`student_id`);
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDate;
Java Code Example:
public class JDBCBatchUpdateExample {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String url ="jdbc:mysql://localhost:3306/my_uat";
String userName="root";
String password ="root123";
String batchInsertQuery ="insert into students values(?,?,?,?)";
Connection connection = DriverManager.getConnection(url,userName,password);
PreparedStatement preparedStatement = connection.prepareStatement(batchInsertQuery);
preparedStatement.setObject(1, null);
preparedStatement.setObject(2, "Harry");
preparedStatement.setObject(3, LocalDate.of(1997, 01, 03));
preparedStatement.setObject(4, "London");
preparedStatement.addBatch();
preparedStatement.setObject(1, null);
preparedStatement.setObject(2, "Ron");
preparedStatement.setObject(3, LocalDate.of(1999, 03, 19));
preparedStatement.setObject(4, "Paris");
preparedStatement.addBatch();
preparedStatement.setObject(1, null);
preparedStatement.setObject(2, "Hermione");
preparedStatement.setObject(3, LocalDate.of(1995, 02, 14));
preparedStatement.setObject(4, "Paris");
preparedStatement.addBatch();
int[] resultSet = preparedStatement.executeBatch();
for(int result: resultSet) {
System.out.println("Result: " +result);
}
}
}
Make sure to add the records to the batch or else nothing will get executed.
Also a common mistake if you do preparedstatement.addBatch() at the end of each record only the last record will get written.
Result:
Java JDBC Batch Update Example
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Install Native M1/M2 Apple Silicon based IntelliJ IDEA IDE on Mac - MacOS
- NewApi error : Finds API accesses to APIs that are not supported in all targeted API versions - Android
- Steps to Enable Developer Mode on iOS 17 - iPhone and iPad - iOS
- Brew Error - This command updates brew itself and does not take formula names - HowTos
- How to check installed Tomcat version - Tomcat
- How to Convert CSV file to SQL Script using Notepad++ - NotepadPlusPlus
- [Android Studio] failed to find Build Tools revision 23.0.0 rc1 - Android-Studio
- Java code to check Internet Connection on Android Device Programmatically - Android