Created
December 9, 2018 21:15
-
-
Save awwsmm/0c8e34fe1e68c526e2df965c02230367 to your computer and use it in GitHub Desktop.
Determine / find the database owner ("dbo") and current user in an embedded Derby (JavaDB) database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.sql.ResultSet; | |
import java.sql.ResultSetMetaData; | |
/** | |
* Example class, showing how to find owner of an embedded Derby database. | |
* | |
* <p>Creates a simple embedded Derby database with a user-specified | |
* {@code databaseName}, and creates a user with the given {@code username} | |
* and {@code password}.</p> | |
* | |
**/ | |
public class GetDerbyDBO { | |
// The `bootPassword` is the password required to start the embedded | |
// database server. It should be entered each time the database is to be | |
// started, and not hardcoded like it is here. | |
private static final String bootPassword = "correcthorsebatterystaple"; | |
private static void usage() { | |
System.out.println("usage: GetDerbyDBO <databaseName> <username> <password>"); | |
} | |
public static void main (String[] args) { | |
if (args.length != 3) { | |
usage(); | |
System.exit(-1); | |
} connectToDB(args); | |
} | |
public static boolean connectToDB (String[] args) { | |
String url = | |
"jdbc:derby:" + args[0] + // embedded database name | |
";user=" + args[1] + // username | |
";password=" + args[2] + // password | |
";bootPassword=" + bootPassword; // password to boot database server | |
Connection connection = null; | |
Statement statement = null; | |
try { // try to connect to the database | |
connection = DriverManager.getConnection(url); | |
System.out.println("connectToDB() : connected to database"); | |
// if there's an exception, the database can't be found | |
} catch (SQLException ex) { | |
url += ";create=true;dataEncryption=true"; | |
try { // try to create the database | |
connection = DriverManager.getConnection(url); | |
System.out.println("connectToDB() : created database"); | |
} catch (SQLException e2) { | |
printSQLException(e2); | |
return false; | |
} | |
} | |
ResultSet resultSet = null; | |
ResultSetMetaData rsmd = null; | |
int columnsNumber = -1; | |
try { | |
statement = connection.createStatement(); | |
// Derby DB configuration can be found in "system tables": | |
// https://db.apache.org/derby/docs/10.9/ref/rrefsistabs38369.html | |
// get the name of the database owner | |
resultSet = statement.executeQuery("select authorizationid from sys.sysschemas where schemaname='SYS'"); | |
rsmd = resultSet.getMetaData(); | |
resultSet.next(); | |
System.out.println("database owner is: " + resultSet.getString(1)); | |
// get the name of the current user | |
resultSet = statement.executeQuery("values current_user"); | |
rsmd = resultSet.getMetaData(); | |
resultSet.next(); | |
System.out.println("current user is: " + resultSet.getString(1)); | |
} catch (SQLException ex) { | |
printSQLException(ex); | |
return false; | |
} | |
return true; | |
} | |
// from: http://bit.ly/2zJV23d | |
public static void printSQLException(SQLException e) { | |
while (e != null) { | |
System.err.println("\n----- SQLException -----"); | |
System.err.println(" SQL State: " + e.getSQLState()); | |
System.err.println(" Error Code: " + e.getErrorCode()); | |
System.err.println(" Message: " + e.getMessage()); | |
e = e.getNextException(); | |
} | |
} | |
} | |
/* EXAMPLE USAGE: | |
$ java GetDerbyDBO myExampleDB dbo dbopassword | |
connectToDB() : created database | |
database owner is: DBO | |
current user is: DBO | |
$ java GetDerbyDBO myExampleDB otherUser anotherPwd | |
connectToDB() : connected to database | |
database owner is: DBO | |
current user is: OTHERUSER | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment