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 12 of 12

Thread: jdbc and HSQL database

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

    Default jdbc and HSQL database

    I am trying to connect to HSQL database using jdbc
    I want to create a properties file with url, username, password and driver.
    I am not sure what to put for the url and driver though, is it just the name of the driver or does it need the path?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: jdbc and HSQL database

    Please be more specific in terms of what your problem is.

    Post the code you're dealing with and the precise problems you're having. If you're weary that Marriott will penalize you for asking for help, then don't be.
    The truth is we can't help unless you give us details.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: jdbc and HSQL database

    jbdc.url=
    jbdc.username=
    jbdc.password=
    jbdc.driver=

    I am creating the .properties file with the details to access the database, i am just not to sure what should be in the url and driver section. I have the driver "hsqldb.jar" so does the driver section just want the driver name, then the url the path? or is there more to it than that.
    Last edited by wdh; October 1st, 2012 at 04:20 PM.

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: jdbc and HSQL database

    jbdc.url=jdbc:hsqldb:file:DBNAME
    jbdc.username=sa
    jbdc.password=
    jbdc.driver=org.hsqldb.jdbcDriver

    Something like this perhaps?
    Be sure to read your laboratory documentation thoroughly, as they usually contain the answers.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: jdbc and HSQL database

    Quote Originally Posted by newbie View Post
    jbdc.url=jdbc:hsqldb:file:DBNAME
    jbdc.username=sa
    jbdc.password=
    jbdc.driver=org.hsqldb.jdbcDriver

    Something like this perhaps?
    Be sure to read your laboratory documentation thoroughly, as they usually contain the answers.
    I have used what you suggested, when trying to connect to the database however, i get this error.

    Exception in thread "main" java.sql.SQLException: The url cannot be null
    	at java.sql.DriverManager.getConnection(DriverManager.java:554)
    	at java.sql.DriverManager.getConnection(DriverManager.java:185)
    	at SimpleDataSource.getConnection(SimpleDataSource.java:48)
    	at TestDB.main(TestDB.java:18)
    Java Result: 1

    The error seems to be coming from here "SimpleDataSource.getConnection(SimpleDataSource.j ava:48)"

        public static Connection getConnection() throws SQLException
        {
            return DriverManager.getConnection(url, username, password);
        }

    The url, username and password are all read in from the properties file.

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: jdbc and HSQL database

    Where do you read in the values from the properties file?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: jdbc and HSQL database

    See post 4 - and pay special attention to the word 'documentation'. A simple google search pulled up the following documentation page:
    Chapter12.Properties
    Have you tried what is described in that page and the API docs?

    Edit: Thread moved from "Whats wrong with my code"
    Last edited by copeg; October 1st, 2012 at 06:07 PM.

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    989
    Thanks
    7
    Thanked 207 Times in 183 Posts

    Default Re: jdbc and HSQL database

    Also at java-forums.org

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

    Default Re: jdbc and HSQL database

    Quote Originally Posted by copeg View Post
    See post 4 - and pay special attention to the word 'documentation'. A simple google search pulled up the following documentation page:
    Chapter12.Properties
    Have you tried what is described in that page and the API docs?

    Edit: Thread moved from "Whats wrong with my code"
    Thank you, I think I may have spotted my mistake after looking through that link. I will update here tomorrow when I have chance to edit and test the new code.

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

    Default Re: jdbc and HSQL database

    I still have no luck with establishing a connection.
    All files are layed out below, I am following Horstmann's example to establish a connection.
    database.properties
    jbdc.url=jdbc:hsqldb:myHSQLDB;
    jbdc.username=sa
    jbdc.password=
    jbdc.driver=org.hsqldb.jdbcDriver

    SimpleDataSource to collect database properties from file
    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;
     
        public static void init(String database)
                throws IOException, ClassNotFoundException
        {
            Properties props = new Properties();
            FileInputStream in = new FileInputStream(database);
            props.load(in);
     
            String driver = props.getProperty("jdbc.driver");
            url = props.getProperty("jdbc.url");
            username = props.getProperty("jdbc.username");
            if(username == null) 
            {
                username = "";
            }
            password = props.getProperty("jdbc.password");
            if(password == null) 
            {
                password = "";
            }
     
     
        }
     
        public static Connection getConnection() throws SQLException
        {
            return DriverManager.getConnection(url, username, password);
        }
     
    }

    TestDB attempts to establish a connection to the database
    import java.sql.Connection;
    public class TestDB 
    {
        public static void main(String[] args) throws Exception
        {
            Connection conn = SimpleDataSource.getConnection();
            conn.close();
        }
    }

    When i try to establish a connection i get this message.

    Exception in thread "main" java.sql.SQLException: The url cannot be null
    	at java.sql.DriverManager.getConnection(DriverManager.java:564)
    	at java.sql.DriverManager.getConnection(DriverManager.java:221)
    	at SimpleDataSource.getConnection(SimpleDataSource.java:48)
    	at TestDB.main(TestDB.java:18)
    Java Result: 1

  11. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: jdbc and HSQL database

    jbdc.url=jdbc:hsqldb:myHSQLDB;
    I don't see you specify the location of the database. The link I posted above explains how to do this...is it a file (jdbc:hsqldb:file:....)? Is it a URL (jdbc:hsqldb:http://...)?

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

    Default Re: jdbc and HSQL database

    It is a file, Yes i have also tried jbdc.url=jdbc:hsqldb:file:myHSQLDB
    But this comes up with the same result. I have also tried using a full pathname but again this returns the same result. My understanding is that the database will be automatically created if it does not exist using the name myHSQLDB which is what i am trying to do. Assuming the rest is correct, shouldn't this work how i have described in the start of this post.

Similar Threads

  1. Connect to MySQL database on remote IP/port through JDBC
    By javauser in forum JDBC & Databases
    Replies: 6
    Last Post: September 7th, 2012, 03:11 AM
  2. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Java Servlet
    Replies: 2
    Last Post: November 21st, 2011, 11:54 PM
  3. jdbc
    By kundalikk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 2nd, 2011, 04:38 AM
  4. Replies: 1
    Last Post: December 2nd, 2010, 05:29 PM
  5. How to update 2 tables in database using JDBC
    By nrao in forum JDBC & Databases
    Replies: 0
    Last Post: November 11th, 2010, 07:05 PM