In today’s tutorial, I am going to share how to write a batch processing program in Java. If you are a Java beginner and want to start learning the Java programming, then keep your close attention in this tutorial as I am going to share how to write a batch processing program in Java.
Copy the below Java program and execute it with the help of Javac compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package logicProgramExamples; import java.io.DataInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class BatchProcessing { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection cn=(Connection) DriverManager.getConnection ("jdbc:mysql://localhost:3306/testdb","webmentor", "#@!Root321"); PreparedStatement smt=cn.prepareStatement("insert into employee values(?,?,?,?)"); DataInputStream kb=new DataInputStream(System.in); String ans=null; do { System.out.println("Enter Employee Id :"); smt.setString(1,kb.readLine()); System.out.println("Enter Employee Name :"); smt.setString(2,kb.readLine()); System.out.println("Enter Employee position :"); smt.setString(3,kb.readLine()); System.out.println("Enter Employee Salary :"); smt.setInt(4,Integer.parseInt(kb.readLine())); System.out.println("Do You Want to Add More Record..\n1.Yes\n2.No"); ans=kb.readLine(); smt.addBatch(); int i[]= smt.executeBatch(); if(i!=null) { System.out.println("Batches Of Record Inserted Successfully."); } else { System.out.println("Batches Of Record Failed To Insert."); } } } catch(Exception e) { System.out.println(e.getMessage());} } } } |
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.