简体   繁体   中英

Derby Embedded Database using Authentication

Apache Derby Embedded Database in default manner it doesn't need authentication. We can enable authentication in system level or database level. I made system level enabling using java code.

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

Then I tried create the database using this connection URL.

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

When i run this

DriverManager.getConnection(connectionURL);

It creates database folder also throw errors regarding authentication? How to create a database with credentials?

java.sql.SQLNonTransientConnectionException: Connection authentication failure occurred. Reason: Invalid authentication.. at 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.generateCsSQLException(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.getConnection(DriverManager.java:208) at derbytest.DerbyTest.createCo nnection(DerbyTest.java:56) at derbytest.DerbyTest.main(DerbyTest.java:39) Caused by: ERROR 08004: Connection authentication failure occurred. Reason: Invalid authentication.. at org.apache.derby.iapi.error.StandardException.newException(Unknown Source) at org.apache.derby.impl.jdbc.SQLExceptionFactory.wrapArgsForTransportAcrossDRDA(Unknown Source) ... 15 more

First specify the database URL that you are going to create with properties create=true

jdbc:derby:derbysample111;create=true

Then get Connection using DriverManager . It will create a database if it is not exist.

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

Then enable authentication in derby and set user and password. It will set 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')");

It is only need to be set once. Then can access your database using this URL

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM