Getting “Unauthorized” exception when trying to create connection in Arango database using Java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Getting “Unauthorized” exception when trying to create connection in Arango database using Java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to create a connection in Arango databse using Java , IDE- Eclipse but while executing the program I am getting the exception “Unauthorized”.

Note: I have logged in to Arango Database with user :root URL- http://127.0.0.1:8529/_db/_system/_admin/aardvark/index.html#logs

Program in Eclipse

    import static com.arangodb.*;
    public class FirstProject {

        public static void main(String[] args) {
            ArangoConfigure configure = new ArangoConfigure();
            configure.init();
            ArangoDriver arangoDriver = new ArangoDriver(configure);
            String dbName = "mydb";
            try { 
              arangoDriver.createDatabase(dbName); 
              System.out.println("Database created: " + dbName);
            } catch (Exception e) { 
              System.out.println("Failed to create database " + dbName + "; " + e.getMessage()); 
            }        
        }
    }

I have used the tutorial https://www.arangodb.com/tutorials/tutorial-java-old/ to write this program and followed all the steps mentioned. Still getting unauthorized exception:
Failed to create database mydb; Unauthorized

Answer

You have to set the user and password (default user “root”, password empty):

ArangoConfigure configure = new ArangoConfigure();
configure.init();
configure.setUser("root");
configure.setPassword("");
ArangoDriver arangoDriver = new ArangoDriver(configure);

In addition I highly suggest you to update to the new java driver (version 4.1.12). Which comes with a new API and a better performance). There is also a tutorial for it (see here).

The required code for your problem in this version would be:

ArangoDB arangoDB = ArangoDB.Builder().user("root").password("").build();
We are here to answer your question about Getting “Unauthorized” exception when trying to create connection in Arango database using Java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji