Embedded Derby Database Using Authentication

The built-in Apache Derby database does not require authentication by default. We can enable authentication at the system level or at the database level. I made a system level using java code.

Properties p=System.getProperties();
p.put("derby.connection.requireAuthentication", "true");

      

Then I tried to create a database using this connection url.

jdbc:derby:derbysample;create=true;user=root;password=root

      

When I run this

DriverManager.getConnection(connectionURL);

      

Does the database folder also generate authentication errors? How do I create a database with credentials?

java.sql.SQLNonTransientConnectionException: Connection authentication failed. Cause. Invalid authentication. org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException (Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException (Unknown Source) at org.apache.derby.impl.jdbc.Util.gene Unknown Source) at org.apache.derby.impl.jdbc.Util.generateCsSQLException (Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException (Unknown Source) at org.apache.derby.impl.jdbc EmbedConnection.checkUserCredentials (Unknown Source) at org.apache.derby.impl.jdbc.EmbedConnection. (Unknown Source) at org.apache.derby.jdbc.InternalDriver.getNewEmbedConnection (Unknown Source) at org.apache.derby.jdbc.InternalDriver.connect (Unknown Source) at org.apache.derby.jdbc.InternalDriver.connect (Unknown Source) at org.apache.derby.jdbc.AutoloadedDriver.connect (Unknown Source) at java.sql.DriverManager.getConnection (DriverManager.java:664) at java.sql.DriverManager.angetConnection (DriverManager.Connection .java: 208) at derbytest.DerbyTest.createConnection (DerbyTest.java:56) at derbytest.DerbyTest.main (DerbyTest.java:39) Caused by: ERROR 08004: Connection authentication failed. Reason: Invalid authentication. org.apache.derby.iapi.error.StandardException.newException (Source Unknown) at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA (Source Unknown) ... 15 moregetConnection (DriverManager.java:208) at derbytest.DerbyTest.createConnection (DerbyTest.java:56) at derbytest.DerbyTest.main (DerbyTest.java:39) Caused by: ERROR 08004: Connection authentication failed. Reason: Invalid authentication. org.apache.derby.iapi.error.StandardException.newException (Source Unknown) at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA (Source Unknown) ... 15 moregetConnection (DriverManager.java:208) at derbytest.DerbyTest.createConnection (DerbyTest.java:56) at derbytest.DerbyTest.main (DerbyTest.java:39) Caused by: ERROR 08004: Connection authentication failed. Reason: Invalid authentication. org.apache.derby.iapi.error.StandardException.newException (Source Unknown) at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA (Source Unknown) ... 15 more

+3


source to share


1 answer


First provide the url of the database you are going to create with properties create=true

jdbc:derby:derbysample111;create=true

      

Then establish a connection with DriverManager

. It will create the database if it doesn't exist.

conn = DriverManager.getConnection("jdbc:derby:derbysample111;create=true");
conn.setSchema("APP");

      



Then turn on authentication in derby and set user and password. It will set up database level authentication.

Statement s = conn.createStatement();
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(\n"
            + "    'derby.connection.requireAuthentication', 'true')");
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(\n"
            + "    'derby.authentication.provider', 'BUILTIN')");
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(\n"
            + "    'derby.user.root', '12345')");
s.executeUpdate("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(\n"
            + "    'derby.database.propertiesOnly', 'true')");

      

You only need to install it once. Then you can access your database using this url

jdbc:derby:derbysample111;create=true;user=root;password=12345

      

+2


source







All Articles