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?
Printable View
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.
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.
Code java: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.
Code :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)"
Code :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?
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"
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
Code :jbdc.url=jdbc:hsqldb:myHSQLDB; jbdc.username=sa jbdc.password= jbdc.driver=org.hsqldb.jdbcDriver
SimpleDataSource to collect database properties from file
Code :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
Code :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.
Code :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://...)?Quote:
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.