|
| 1 | +/* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.*/ |
| 2 | + |
| 3 | +/* |
| 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. |
| 8 | + |
| 9 | + NOTES Use JDK 1.8 and above |
| 10 | + |
| 11 | + MODIFIED (MM/DD/YY) |
| 12 | + nbsundar 02/04/21 - Creation (Contributor - kmensah) |
| 13 | + */ |
| 14 | +import java.sql.Connection; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.sql.Statement; |
| 17 | + |
| 18 | +import oracle.ucp.jdbc.PoolDataSourceFactory; |
| 19 | +import oracle.ucp.jdbc.PoolDataSource; |
| 20 | + |
| 21 | +public class CreateUser { |
| 22 | + // Update the |
| 23 | + final static String DB_URL="jdbc:oracle:thin:@//localhost:1521/XEPDB1"; |
| 24 | + // Enter the admin database username associated with your XE installation |
| 25 | + // It is usually "sys as sysdba" |
| 26 | + final static String AdminUSER = "<yourDBUser>"; |
| 27 | + // Enter the password for the admin user |
| 28 | + final static String AdminPASSWORD = "<yourDBPassword>"; |
| 29 | + |
| 30 | + |
| 31 | + // Enter the new database user that you want to create |
| 32 | + final static String newDBUser = "<db-new-username>"; |
| 33 | + // Enter the password for the new database user that you want to create |
| 34 | + final static String newDBPassword = "<db-new-password>"; |
| 35 | + final static String CONN_FACTORY_CLASS_NAME="oracle.jdbc.pool.OracleDataSource"; |
| 36 | + |
| 37 | + /* |
| 38 | + * The sample demonstrates UCP as client side connection pool. |
| 39 | + */ |
| 40 | + public static void main(String args[]) throws Exception { |
| 41 | + // Get the PoolDataSource for UCP |
| 42 | + PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource(); |
| 43 | + // Set the connection factory first before all other properties |
| 44 | + pds.setConnectionFactoryClassName(CONN_FACTORY_CLASS_NAME); |
| 45 | + pds.setURL(DB_URL); |
| 46 | + pds.setUser(DB_USER); |
| 47 | + pds.setPassword(DB_PASSWORD); |
| 48 | + pds.setConnectionPoolName("JDBC_UCP_POOL"); |
| 49 | + |
| 50 | + String createUserSQL = "BEGIN " + |
| 51 | + "EXECUTE IMMEDIATE ('CREATE USER " + newDBUser + " IDENTIFIED BY " + newDBPassword + |
| 52 | + " DEFAULT TABLESPACE USERS QUOTA UNLIMITED ON USERS'); " + |
| 53 | + "EXECUTE IMMEDIATE ('GRANT CREATE SESSION, CREATE VIEW, CREATE SEQUENCE, " + |
| 54 | + " CREATE PROCEDURE, CREATE TABLE, CREATE TRIGGER, CREATE TYPE, " + |
| 55 | + " CREATE MATERIALIZED VIEW TO " + newDBUser + "'); " + |
| 56 | + "END;"; |
| 57 | + |
| 58 | + // Default is 0. Set the initial number of connections to be created |
| 59 | + // when UCP is started. |
| 60 | + pds.setInitialPoolSize(5); |
| 61 | + |
| 62 | + // Minimum number of connections that is maintained by UCP at runtime |
| 63 | + pds.setMinPoolSize(5); |
| 64 | + |
| 65 | + // Maximum number of connections allowed on the connection pool |
| 66 | + pds.setMaxPoolSize(20); |
| 67 | + |
| 68 | + // Get the database connection from UCP. |
| 69 | + try (Connection conn = pds.getConnection()) { |
| 70 | + conn.setAutoCommit(false); |
| 71 | + // Prepare a statement to execute the SQL Queries. |
| 72 | + Statement statement = conn.createStatement(); |
| 73 | + // Create a table CUSTOMER |
| 74 | + statement.executeUpdate(createUserSQL); |
| 75 | + System.out.println("New Database user " + newDBUser + " created"); |
| 76 | + } catch (SQLException e) { |
| 77 | + System.out.println("QuickStart - " |
| 78 | + + "CreateUser - SQLException occurred : " + e.getMessage()); |
| 79 | + } |
| 80 | + } // End of main |
| 81 | +} // End of CreateUser |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments