22
33/*
44 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.
88
99 NOTES Use JDK 1.8 and above
1010
1111 MODIFIED (MM/DD/YY)
12- nbsundar 02/04 /21 - Creation (Contributor - kmensah)
12+ nbsundar 02/10 /21 - Creation (Contributor - kmensah)
1313 */
1414import java .sql .Connection ;
1515import java .sql .SQLException ;
1919import oracle .ucp .jdbc .PoolDataSource ;
2020
2121public 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.
2324 final static String DB_URL ="jdbc:oracle:thin:@//localhost:1521/XEPDB1" ;
2425 // Enter the admin database username associated with your XE installation
2526 // It is usually "sys as sysdba"
26- final static String AdminUSER = "<yourDBUser >" ;
27+ final static String AdminUSER = "<DBAdminUser >" ;
2728 // Enter the password for the admin user
28- final static String AdminPASSWORD = "<yourDBPassword >" ;
29+ final static String AdminPASSWORD = "<DBAdminPassword >" ;
2930
3031
3132 // Enter the new database user that you want to create
@@ -35,7 +36,9 @@ public class CreateUser {
3536 final static String CONN_FACTORY_CLASS_NAME ="oracle.jdbc.pool.OracleDataSource" ;
3637
3738 /*
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.
3942 */
4043 public static void main (String args []) throws Exception {
4144 // Get the PoolDataSource for UCP
@@ -47,6 +50,7 @@ public static void main(String args[]) throws Exception {
4750 pds .setPassword (DB_PASSWORD );
4851 pds .setConnectionPoolName ("JDBC_UCP_POOL" );
4952
53+ // Create a new database user along with granting the required privileges.
5054 String createUserSQL = "BEGIN " +
5155 "EXECUTE IMMEDIATE ('CREATE USER " + newDBUser + " IDENTIFIED BY " + newDBPassword +
5256 " DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS'); " +
@@ -55,8 +59,7 @@ public static void main(String args[]) throws Exception {
5559 " CREATE MATERIALIZED VIEW TO " + newDBUser + "'); " +
5660 "END;" ;
5761
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.
6063 pds .setInitialPoolSize (5 );
6164
6265 // Minimum number of connections that is maintained by UCP at runtime
@@ -70,7 +73,7 @@ public static void main(String args[]) throws Exception {
7073 conn .setAutoCommit (false );
7174 // Prepare a statement to execute the SQL Queries.
7275 Statement statement = conn .createStatement ();
73- // Create a table CUSTOMER
76+ // Create a new database user and grant privileges
7477 statement .executeUpdate (createUserSQL );
7578 System .out .println ("New Database user " + newDBUser + " created" );
7679 } catch (SQLException e ) {
0 commit comments