forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDerbyBatchUpdates.java
More file actions
67 lines (49 loc) · 2.16 KB
/
DerbyBatchUpdates.java
File metadata and controls
67 lines (49 loc) · 2.16 KB
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
58
59
60
61
62
63
64
65
66
67
package com.zetcode;
import com.zetcode.utils.DBUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DerbyBatchUpdates {
public static void main(String[] args) {
Connection con = null;
Statement st = null;
String url = "jdbc:derby://localhost:1527/testdb";
String user = "app";
String password = "app";
try {
con = DriverManager.getConnection(url, user, password);
con.setAutoCommit(false);
st = con.createStatement();
st.addBatch("DELETE FROM CARS");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Audi', 52642)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Mercedes', 57127)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Skoda', 9000)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Volvo', 29000)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Bentley', 350000)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Citroen', 21000)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Hummer', 41400)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Volkswagen', 21600)");
st.addBatch("INSERT INTO CARS(NAME, PRICE) VALUES('Jaguar', 95000)");
int counts[] = st.executeBatch();
con.commit();
System.out.println("Committed " + counts.length + " updates");
} catch (SQLException ex) {
try {
if (con != null) {
con.rollback();
}
} catch (SQLException ex2) {
Logger lgr = Logger.getLogger(DerbyBatchUpdates.class.getName());
lgr.log(Level.FINEST, ex2.getMessage(), ex2);
}
Logger lgr = Logger.getLogger(DerbyBatchUpdates.class.getName());
lgr.log(Level.FINEST, ex.getMessage(), ex);
} finally {
DBUtils.closeStatement(st);
DBUtils.closeConnection(con);
}
}
}