-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathUpdate.java
More file actions
63 lines (46 loc) · 2.09 KB
/
Update.java
File metadata and controls
63 lines (46 loc) · 2.09 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
package movie;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import oracle.jdbc.OracleType;
import oracle.sql.json.OracleJsonFactory;
import oracle.sql.json.OracleJsonObject;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;
/**
* Updates an movie record using whole document replacement.
*
* <p>
* Run first: {@link CreateTable}, {@link Insert}
* </p>
*/
public class Update {
public static void main(String[] args) throws Exception {
OracleJsonFactory factory = new OracleJsonFactory();
PoolDataSource pool = PoolDataSourceFactory.getPoolDataSource();
pool.setURL(String.join("", args));
pool.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
try (Connection con = pool.getConnection()) {
PreparedStatement stmt = con.prepareStatement(
"SELECT m.data FROM movie m WHERE m.data.name.string() = :1");
stmt.setString(1, "Iron Man");
ResultSet rs = stmt.executeQuery();
rs.next();
OracleJsonObject obj = rs.getObject(1, OracleJsonObject.class);
System.out.println(obj);
// At this point, obj is a direct pointer into binary JSON sent
// from the server and is immutable. Calling put() on the object
// would raise an error. The next line makes a mutable copy:
obj = factory.createObject(obj);
obj.put("gross", obj.getBigDecimal("gross").add(BigDecimal.valueOf(1000_000)));
PreparedStatement update = con.prepareStatement(
"UPDATE movie m SET m.data = :1 WHERE m.data.name.string() = :2");
update.setObject(1, obj, OracleType.JSON);
update.setString(2, "Iron Man");
update.execute();
System.out.println("Iron Man's gross is updated");
System.out.println(obj);
}
}
}