2
2
3
3
/*
4
4
DESCRIPTION
5
- The code sample creates a new database user.
6
- (a) Make sure to provide the connection URL and the admin username and password.
7
- (b) Provide a new user and password that you want to create.
5
+ The code sample creates a new database user and grants the required privileges .
6
+ (a) Edit this file and update the connection URL along with the admin username and password.
7
+ (b) Also, provide a new database user and password that you want to create.
8
8
9
9
NOTES Use JDK 1.8 and above
10
10
11
11
MODIFIED (MM/DD/YY)
12
- nbsundar 02/04 /21 - Creation (Contributor - kmensah)
12
+ nbsundar 02/10 /21 - Creation (Contributor - kmensah)
13
13
*/
14
14
import java .sql .Connection ;
15
15
import java .sql .SQLException ;
19
19
import oracle .ucp .jdbc .PoolDataSource ;
20
20
21
21
public class CreateUser {
22
- // Update the
22
+ // The following connection string is pointing to Oracle XE database.
23
+ // Change this URL to the specific connnection string that you have.
23
24
final static String DB_URL ="jdbc:oracle:thin:@//localhost:1521/XEPDB1" ;
24
25
// Enter the admin database username associated with your XE installation
25
26
// It is usually "sys as sysdba"
26
- final static String AdminUSER = "<yourDBUser >" ;
27
+ final static String AdminUSER = "<DBAdminUser >" ;
27
28
// Enter the password for the admin user
28
- final static String AdminPASSWORD = "<yourDBPassword >" ;
29
+ final static String AdminPASSWORD = "<DBAdminPassword >" ;
29
30
30
31
31
32
// Enter the new database user that you want to create
@@ -35,7 +36,9 @@ public class CreateUser {
35
36
final static String CONN_FACTORY_CLASS_NAME ="oracle.jdbc.pool.OracleDataSource" ;
36
37
37
38
/*
38
- * The sample demonstrates UCP as client side connection pool.
39
+ * Sample to create a new database user and password and grant the required privileges.
40
+ * As a required step, you will need to provide the database connection string,
41
+ * admin username and password to create a new database user.
39
42
*/
40
43
public static void main (String args []) throws Exception {
41
44
// Get the PoolDataSource for UCP
@@ -47,6 +50,7 @@ public static void main(String args[]) throws Exception {
47
50
pds .setPassword (DB_PASSWORD );
48
51
pds .setConnectionPoolName ("JDBC_UCP_POOL" );
49
52
53
+ // Create a new database user along with granting the required privileges.
50
54
String createUserSQL = "BEGIN " +
51
55
"EXECUTE IMMEDIATE ('CREATE USER " + newDBUser + " IDENTIFIED BY " + newDBPassword +
52
56
" DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS'); " +
@@ -55,8 +59,7 @@ public static void main(String args[]) throws Exception {
55
59
" CREATE MATERIALIZED VIEW TO " + newDBUser + "'); " +
56
60
"END;" ;
57
61
58
- // Default is 0. Set the initial number of connections to be created
59
- // when UCP is started.
62
+ // Initial number of connections to be created when UCP is started.
60
63
pds .setInitialPoolSize (5 );
61
64
62
65
// Minimum number of connections that is maintained by UCP at runtime
@@ -70,7 +73,7 @@ public static void main(String args[]) throws Exception {
70
73
conn .setAutoCommit (false );
71
74
// Prepare a statement to execute the SQL Queries.
72
75
Statement statement = conn .createStatement ();
73
- // Create a table CUSTOMER
76
+ // Create a new database user and grant privileges
74
77
statement .executeUpdate (createUserSQL );
75
78
System .out .println ("New Database user " + newDBUser + " created" );
76
79
} catch (SQLException e ) {
0 commit comments