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?
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.
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?
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
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 03:20 PM.
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
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.
Where do you read in the values from the properties file?
Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code
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 05:07 PM.
Also at java-forums.org
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
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://...)?jbdc.url=jdbc:hsqldb:myHSQLDB;
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.