Search code examples
javajdbcderby

Enable an User Authentication in EmbeddedD Derby


I have a program that has a menu with two options first,to create a new database second,open existing database

my database creation part code is as follows

public EmbeddedDerby(String dbName, String userName, String pass) throws SQLException {
        String protocol = "jdbc:derby:";

        conn = DriverManager.getConnection(protocol + "dist/" + dbName + ""
                + ";create=true;user=" + userName + " " + ";password=" + pass + "");
        st = conn.createStatement();
        dbmd = conn.getMetaData();
        rs = dbmd.getTables(null, "APP", "DBNAME", null);
        String sqlTabel = "CREATE TABLE APP.DBNAME"
                + "(NAME VARCHAR(255) not null primary key,"
                + "TEL   VARCHAR(10))";
        st.execute(sqlTabel);
        }

my database opening part is as follows

 public void openDataBase(String dbName, String userName, String pass) throws SQLException {
        String protocol = "jdbc:derby:";
        conn = DriverManager.getConnection(protocol + "dist/" + dbName + ""
                + ";user=" + userName + ";password=" + pass + "");
        st = conn.createStatement();
        }

I have no issue with creating a new database, but when I want to open existing database, it does not check whether I enter a right username and password or not. Moreover, it accepts any username and password as long as name of a database exists. I thought getConnection method will check correctness of username and password, yet it seems I am totally wrong. Could anyone tell me what is wrong and how I can fix this issue?


Solution

  • System Level set properties using java to enable authenticatication.

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

    Database Level set properties to enable authentication.

    CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(
        'derby.connection.requireAuthentication',
        'true')