slu

Søren's Blog

Random Ramblings

Previous Entry Share Next Entry
HSQLDB Part 2 - a very simple example
computer
slu
The following is a very simple example of how to connect to and retrieve data from the HSQLDB database we created in part 1.

Note: This is not production quality code. It's just a simple demo/test.

You need to change the path marked in bold. Save the file as TestDemobase.java and compile it (javac TestDemobase.java). Run it using java TestDemobase. This requrires that the CLASSPATH environment variable contains the hsqldb.jar.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestDemobase {
    static String database = "/path/to/your/app/demobase";
    static String sql = "SELECT title, url FROM Bookmarks ORDER BY title";
    static Connection connection;

    public static void main(String[] args) throws Exception {
	System.out.println("TestDemobase\n");	
	
	Class.forName("org.hsqldb.jdbcDriver");
	connection = DriverManager.getConnection("jdbc:hsqldb:" + database, "sa", "");

	Statement statement = null;
        ResultSet resultSet = null;

        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);

	while (resultSet.next()) {
	    System.out.println(resultSet.getString("title") + " (" +
			       resultSet.getString("url") + ")");
	    
	}
	
	resultSet.close();
	statement.close();
	connection.close();
    }
}

  • 1

Same example using Jython

(Anonymous)
Jython is a nice way to try out little things like this:
C:\TEMP>set CLASSPATH=hsqldb.jar;%CLASSPATH%

C:\TEMP>jython
*sys-package-mgr*: processing modified jar, 'C:\TEMP\hsqldb.jar'
Jython 2.1 on java1.4.2_04 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> from java.lang import Class
>>> Class.forName("org.hsqldb.jdbcDriver");
[Error: Irreparable invalid markup ('<jclass [...] org.hsqldb.jdbcdriver>') in entry. Owner must fix manually. Raw contents below.]

Jython is a nice way to try out little things like this:
<pre>
C:\TEMP>set CLASSPATH=hsqldb.jar;%CLASSPATH%

C:\TEMP>jython
*sys-package-mgr*: processing modified jar, 'C:\TEMP\hsqldb.jar'
Jython 2.1 on java1.4.2_04 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>> <b>from java.lang import Class</b>
>>> <b>Class.forName("org.hsqldb.jdbcDriver");</b>
<jclass org.hsqldb.jdbcDriver at 30732921>
>>> <b>from java.sql import Statement, ResultSet, DriverManager</b>
>>> <b>connection = DriverManager.getConnection("jdbc:hsqldb:c:\\temp\\hsql","sa","")</b>
>>> <b>statement = connection.createStatement()</b>
>>> <b>resultSet = statement.executeQuery("SELECT title, url FROM Bookmarks ORDER BY title")</b>
>>> <b>while resultSet.next():</b>
... <b>print "%s (%s)" % (resultSet.getString("title"), resultSet.getString("url"))</b>
...<font color="green">
Apache Jakarta Tomcat (http://jakarta.apache.org/tomcat/)
HSQLDB 100% Java Database (http://hsqldb.sourceforge.net/)
Java Technology (http://java.sun.com/)</font>
>>> <b>resultSet.close();</b>
>>> <b>statement.close();</b>
>>> <b>connection.close();</b>
>>>
</pre>
- jeoff wilks

create=true;

(Anonymous)
static String database = "D:/demobase;create=true;";

connection = DriverManager.getConnection("jdbc:hsqldb:" + database, "sa", "");

If you use above database name you can create new DATABASE connection .
............
Mrunalini wankhade

Re: create=true;

(Anonymous)
I get this error when I try running the example.
java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver

I set classpath=.;C:\hsqldb\lib\hsqldb.jar ;

Here's part of the code, up until the error line.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

class HSQLTest {
static String database = "/c:/";
static String sql = "SELECT title, url FROM Bookmarks ORDER BY title";
static Connection connection;


public static void main(String [] args) {
try {
Class.forName("org.hsqldb.jdbcDriver");

. . .

What's wrong?

Re: create=true;

(Anonymous)
It worked when I did this at the prmpt
C:\set classpath="c:\hsqldb\lib\hsqldb.jar"

Thanks

Re: create=true;

(Anonymous)
Actually, this is what made it work.
C:\>java -classpath "C:\hsqldb\lib\hsqldb.jar;.;" HSQLTest

Hi...

(Anonymous)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;

public class Demobase {
static String sql = "SELECT name, occupation FROM people";
static Connection connection;

public static void main(String[] args) throws Exception {
System.out.println("Demobase\n");

Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:Demobase");

Statement statement = null;
ResultSet resultSet = null;

statement = connection.createStatement();
statement.executeUpdate("DROP TABLE IF EXISTS people;");
statement.executeUpdate("CREATE TABLE people(name VARCHAR, occupation VARCHAR)");
PreparedStatement prep = connection.prepareStatement(
"INSERT INTO people VALUES (?, ?);");

prep.setString(1, "Mehmet");
prep.setString(2, "Java Developer");
prep.addBatch();
connection.setAutoCommit(false);
prep.executeBatch();
connection.setAutoCommit(true);
resultSet = statement.executeQuery(sql);

while (resultSet.next()) {
System.out.println(resultSet.getString("name") + ": " +
resultSet.getString("occupation"));

}

resultSet.close();
statement.close();
connection.close();
}
}

at this from me...

  • 1
?

Log in

No account? Create an account