I have read the Derby documentation but couldn't find any solution. I want to set a user id and password for my database in my application.
I have provided the user and password option in the connection URL but when I try to open it again with any password it is still opening. The URL is below
DriverManager.getConnection(
"jdbc:derby:database;user=admin;password=1234;create=true;");
Only database url with username and password will not work. You have to setup a embedded database first then set different properties to enable user authentication using Derby's built-in user authentication and user authorization.
You can read about derby authorization on its documentation- https://db.apache.org/derby/docs/10.4/devguide/cdevcsecure36595.html#cdevcsecure36595
You have to enable authentication after creating the embedded database.
You can use this method to setup authentication--
public static void turnOnBuiltInUsers(Connection conn) throws SQLException {
System.out.println("Turning on authentication.");
Statement s = conn.createStatement();
// Setting and Confirming requireAuthentication
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.connection.requireAuthentication', 'true')");
ResultSet rs = s.executeQuery(
"VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(" +
"'derby.connection.requireAuthentication')");
rs.next();
System.out.println("Value of requireAuthentication is " +
rs.getString(1));
// Setting authentication scheme to Derby
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.authentication.provider', 'BUILTIN')");
// Creating some sample users
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.user.sa', 'ajaxj3x9')");
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.user.guest', 'java5w6x')");
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.user.mary', 'little7xylamb')");
// Setting default connection mode to no access
// (user authorization)
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.database.defaultConnectionMode', 'noAccess')");
// Confirming default connection mode
rs = s.executeQuery (
"VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(" +
"'derby.database.defaultConnectionMode')");
rs.next();
System.out.println("Value of defaultConnectionMode is " +
rs.getString(1));
// Defining read-write users
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.database.fullAccessUsers', 'sa,mary')");
// Defining read-only users
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.database.readOnlyAccessUsers', 'guest')");
// Confirming full-access users
rs = s.executeQuery(
"VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(" +
"'derby.database.fullAccessUsers')");
rs.next();
System.out.println("Value of fullAccessUsers is " + rs.getString(1));
// Confirming read-only users
rs = s.executeQuery(
"VALUES SYSCS_UTIL.SYSCS_GET_DATABASE_PROPERTY(" +
"'derby.database.readOnlyAccessUsers')");
rs.next();
System.out.println("Value of readOnlyAccessUsers is " +
rs.getString(1));
// We would set the following property to TRUE only
// when we were ready to deploy.
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(" +
"'derby.database.propertiesOnly', 'false')");
s.close();
}
You can use this link for complete a quick example of the same.