-
Notifications
You must be signed in to change notification settings - Fork 49
/
BatchProcessingExample.java
40 lines (32 loc) · 1.46 KB
/
BatchProcessingExample.java
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
import java.sql.*;
public class BatchProcessingExample {
public static void main(String[] args) {
// connecting to the database
String serverName = "localhost";
String myDatabase = "JavaJDBC";
final String url = "jdbc:mysql://" + serverName + "/" + myDatabase;
// username and password
final String username = "root";
final String password = "MyNewPass";
try {
// using static method of DriverManager
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Success: Database connection. " + connection);
// creating statement
Statement stmt = connection.createStatement();
String SQL = "INSERT INTO MyGuests(firstname, lastname, email, reg_date) values('Mariam','Varkey','[email protected]','2019-03-12')";
// adding to batch
stmt.addBatch(SQL);
// creating new SQL query
SQL = "INSERT INTO MyGuests(firstname, lastname, email, reg_date) values('Anurag','BG','[email protected]','2018-12-12')";
// adding to batch
stmt.addBatch(SQL);
// executing batch
int count[] = stmt.executeBatch();
for (int i = 0; i < count.length; i++)
System.out.println(count[i]);
} catch (SQLException e) {
System.out.println("Error: Couldn't connect to database " + e);
}
}
}