问题
Im planning to learn jdbc topic. I have downloaded the HSQL DB from the site and extracted the zip file. I have few queries:
How do we create a new database?
Will we be able to see the contents of the db via any tool [ just like we see in oracle ]?
How to know what is the driver name they have supplied?
I have checked the weblink but could not find any solution. Please suggest.
Thanks, Pavan.
回答1:
http://hsqldb.org/web/hsqlFAQ.html#NEWDB
How to Create a new database:
A new database is created automatically if it does not yet exist. Just connect to the not-yet-existing database using the jdbc:hsqldb:file:«database-path» URL (should replace the last part with the path you want) with the user 'sa' (or any name) and a password (can be an empty string). You will use this name and password to connect again.
Q: Will we be able to see the contents of the db via any tool?
No. SQL Server tools aren't going to work with Oracle, mySQL tools aren't going to work with SQL Server ... and the primary goal of HSQLDB it to use it with JDBC etc, i.e. programmatic connections.
Q: How to know what is the driver name they have supplied?
From the FAQs:
HSQLDB comes with documentation, example program source code that can help programers who are new to JDBC programming.
Basic sample programs are in the /src/org/hsqldb/sample folder.
Source code of test programs are useful examples of how to use different features of JDBC and SQL. Check the sources in the /src/org/hsqldb/test folder.
SQL test scripts are in the /runtest folder and offer extensive examples of SQL statements.
HSQLDB has a standard JDBC interface. HSQLDB specific JDBC documentation is included in the /doc/src folder.
The driver file is "hsqldb.jar".
回答2:
Read the complete tutorial on their documentation site here. But below are the two important steps. I, however suggest you to ue Java DB that comes with the JDK itself from JDK 1.6 on wards. Here is the official documentation.
// Load the HSQL Database Engine JDBC driver
// hsqldb.jar should be in the class path or made part of the current jar
Class.forName("org.hsqldb.jdbcDriver");
// connect to the database. This will load the db files and start the
// database if it is not alread running.
// db_file_name_prefix is used to open or create files that hold the state
// of the db.
// It can contain directory names relative to the
// current working directory
conn = DriverManager.getConnection("jdbc:hsqldb:"
+ db_file_name_prefix, // filenames
"sa", // username
""); // password
}
回答3:
Are you talking about in memory or real databases? If in memory, I've only done it using hibernate to create the database. Just set hibernate to create and it will create the tables for you.
Otherwise, can't help.
来源:https://stackoverflow.com/questions/8716450/how-to-connect-to-hsqldb