forked from oracle-samples/oracle-db-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkers_jdbc.java
More file actions
136 lines (112 loc) · 4.06 KB
/
Workers_jdbc.java
File metadata and controls
136 lines (112 loc) · 4.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
/------------------------------------------------------------------------------
/ Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
/
/ Portions Copyright 2006-2015, Kuassi Mensah. All rights reserved.
/ https://www.amazon.com/dp/1555583296
/ Adapated from existing JDBC demo.
/
/------------------------------------------------------------------------------
/ DESCRIPTION
/ The following code sample runs as a stand-alone JDBC client usingJ RE or JDK.
/ It retrieves a worker from a database, then updates its position and salary.
*/
import java.sql.*;
import oracle.jdbc.driver.*;
public class Workers_jdbc
{
public static void main (String args []) throws SQLException {
String name = null;
String pos = null;
int sal;
int id;
long t0,t1;
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
if ( args.length < 1 ) {
System.err.println("Usage: Java Workers <wid> <new position> <new salary>");
System.exit(1);
}
// Get parameters value
id = Integer.parseInt(args[0]);
pos = args[1];
sal = Integer.parseInt(args[2]);
/*
* Where is your code running: in the database or outside?
*/
if (System.getProperty("oracle.jserver.version") != null)
{
/*
* You are in the database, already connected, use the default
* connection
*/
conn = DriverManager.getConnection("jdbc:default:connection:");
System.out.println ("Running in OracleJVM, in the database!");
}
else
{
/*
* You are not in the database, you need to connect to
* the database
*/
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:",
"scott", "tiger");
System.out.println ("Running in JDK VM, outside the database!");
}
/*
* Auto commit is off by default in OracleJVM (not supported)
* Set auto commit off in JDBC case
*/
conn.setAutoCommit (false);
// Start timing
t0=System.currentTimeMillis();
/*
* find the name of the workers given his id number
*/
// create statement
stmt = conn.createStatement();
// find the name of the worker
ResultSet rset = stmt.executeQuery(
"SELECT WNAME FROM workers WHERE wid = " + id);
// retrieve and print the result (we are only expecting 1 row
while (rset.next())
{
name = rset.getString(1);
}
// return the name of the worker who has the given worker number
System.out.println ("Worker Name: "+ name);
/*
* update the position and salary of the retrieved worker
*/
// prepare the update statement
pstmt = conn.prepareStatement("UPDATE WORKERS SET WPOSITION = ?, " +
" WSALARY = ? WHERE WNAME = ?");
// set up bind values and execute the update
pstmt.setString(1, pos);
pstmt.setInt(2, sal);
pstmt.setString(3, name);
pstmt.execute();
// double-check (retrieve) the updated position and salary
rset = stmt.executeQuery(
"SELECT WPOSITION, WSALARY FROM WORKERS WHERE WNAME = '" +
name + "'");
while (rset.next())
{
pos = rset.getString ("wposition");
sal = rset.getInt ("wsalary");
}
System.out.println ("Worker: Id = " + id + ", Name = " + name +
", Position = " + pos + ", Salary = " + sal);
// Close the ResultSet
rset.close();
// Close the Statement
stmt.close();
// Stop timing
t1=System.currentTimeMillis();
System.out.println ("====> Duration: "+(int)(t1-t0)+ " Milliseconds");
// Close the connection
conn.close();
}
}