Søren Lund (slu) wrote,
Søren Lund
slu

  • Mood:

HSQLDB Part 2 - a very simple example

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();
    }
}
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 7 comments