Search code examples
javaoracle12c

Is it possible to create a new PDB from java?


For one of my poc, I would like to create a new PDB from java.

I am able to get cdb connection say,

Connection cdbConnection = DBUtils.getDBConnection(Constants.DB_CDB_NAME, Constants.DB_CDB_USER_NAME, Constants.DB_CDB_PASSWORD);

Now am trying to clone PDB3: CREATE PLUGGABLE DATABASE PDB5 FROM PDB3;

public static void createPluggableDB(Connection cdbConnection) {
  try (Statement stmt = cdbConnection.createStatement();) {
     stmt.executeUpdate("CREATE PLUGGABLE DATABASE PDB5 FROM PDB3;");
  } catch (SQLException exception) {
    // exception handling
  }
}

When I execute this, am getting,

java.sql.SQLSyntaxErrorException: ORA-00922: missing or invalid option
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:494)
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1052)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:537)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:255)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:610)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:213)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:37)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:887)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1136)
at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1754)
at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1709)
at oracle.jdbc.driver.OracleStatementWrapper.execute(OracleStatementWrapper.java:300)
at com.xit.gtw.db.DBUtils.executeDBScripts(DBUtils.java:43)
at PluggableDBBuilder.createPluggableDB(PluggableDBBuilder.java:51)
at PluggableDBBuilder.main(PluggableDBBuilder.java:35)

Is it possible to create a PDB from java?

Thanks in advance!


Solution

  • You shouldn't have the semicolon in the string you pass to executeUpdate(); it's a statement separator not part of the statement. So remove that:

    stmt.executeUpdate("CREATE PLUGGABLE DATABASE PDB5 FROM PDB3");
    

    That applies to any statement you try to execute through JDBC (or, indeed, through many other mechanisms - including execute immediate).