Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Database authorisation problems

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Database authorisation problems

    Ok this problem has confused me, i will outline what the program does first before i tell you the issue i am having.
    I have set up HSQLDB i have created a program which reads from my database.properties file and connects to the database.
    I then use this connection to perform some SQL commands such as add, remove and update entries from a simple console menu. If i enter my connection details manually so the url, username, password and driver it connects fine. When doing it manually i have username and password both as "", so the value is null. It all works fine then, my menu works, i can add, delete, update and display my table. If however, i try and connect by directing my program to the details in my database.properties file which also has a blank username and password field i cannot connect, i get this error message.

    Exception in thread "main" java.sql.SQLInvalidAuthorizationSpecException: invalid authorization specification - not found: SA

    I wanted to see what values were being used to connect so i put a println in to display the username and password when entering my details manually and also from reading from the file.
    So when i attempt to establish a connection it shows me this line first. They both display username=null and password= null but if i link to the database.properties file i get the error message mentioned above . . . .It doesn't make sense to me as if a password/username were required, why can i connect by manually setting these to "" in my connection program?

    Below is the connection program, you can see in the getConnection method i have the username and password field set to "". To use the values read from the file i use username and password instead.
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
     
    public class SimpleDataSource 
    {
        private static String url;
        private static String username;
        private static String password;
        private static String driver;
     
        public static void init(String database)
                throws IOException, ClassNotFoundException
        {
            Properties props = new Properties();
            FileInputStream in = new FileInputStream(database);
            props.load(in);
     
            driver = props.getProperty("jbdc.driver");
            url = props.getProperty("jbdc.url");
            username = props.getProperty("jbdc.username");
            if(username == null) 
            {
                username = "";
            }
            password = props.getProperty("jbdc.password");
            if(password == null) 
            {
                password = "";
            };
     
     
     
        }
     
        public static Connection getConnection() throws SQLException
        {
            System.out.println("Username:" + username + "Password:" + password);
            return DriverManager.getConnection("jdbc:hsqldb:file:myHSQLDB", "", "");
        }
     
    }

    Problem is now solved, i failed to call the init method.
    Last edited by wdh; October 10th, 2012 at 02:53 PM.


Similar Threads

  1. Problems connecting to mysql database on network
    By 2k. in forum JDBC & Databases
    Replies: 10
    Last Post: May 8th, 2012, 08:54 PM
  2. Replies: 1
    Last Post: December 15th, 2011, 08:29 AM
  3. [SOLVED] Have a few odd problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: October 19th, 2010, 06:08 PM
  4. problems with connection to mysql database
    By thepower in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 20th, 2010, 02:59 AM
  5. Replies: 4
    Last Post: September 19th, 2009, 11:56 PM