How to Setup HSQLDB Connection and Update

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Setup HSQLDB Connection and Update

This Java example program shows how to setup hsqldb connection and update.

[edit] Java Source Code

  • Package: example.jdbc.hsql
  • File: MainClass.java
package example.jdbc.hsql;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
 
public class MainClass {
    public static void main(String[] args) {
	Connection connection = null;
	Statement statement = null;
	try {
	    Class.forName("org.hsqldb.jdbcDriver").newInstance();
	    String url = "jdbc:hsqldb:hsqldb\\demoDatabase";
	    connection = DriverManager.getConnection(url, "username", "password");
	    connection.setAutoCommit(false);
 
	    statement = connection.createStatement();
 
	    String update1 = "UPDATE employees SET email = 'a@b.com' WHERE email = 'a@a.com'";
	    statement.executeUpdate(update1);
	    Savepoint savepoint1 = connection.setSavepoint("savepoint1");
 
	    String update2 = "UPDATE employees SET email = 'b@b.com' WHERE email = 'b@c.com'";
	    statement.executeUpdate(update2);
	    Savepoint savepoint2 = connection.setSavepoint("savepoint2");
 
	    String update3 = "UPDATE employees SET email = 'c@c.com' WHERE email = 'c@d.com'";
	    statement.executeUpdate(update3);
	    Savepoint savepoint3 = connection.setSavepoint("savepoint3");
 
	    String update4 = "UPDATE employees SET email = 'd@d.com' WHERE email = 'd@e.com'";
	    statement.executeUpdate(update4);
	    Savepoint savepoint4 = connection.setSavepoint("savepoint4");
 
	    String update5 = "UPDATE employees SET email = 'e@e.com' WHERE email = 'e@f.com'";
	    statement.executeUpdate(update5);
	    Savepoint savepoint5 = connection.setSavepoint("savepoint5");
 
	    connection.rollback(savepoint3);
	    connection.commit();
	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    if (statement != null) {
		try {
		    statement.close();
		} catch (SQLException e) {
		} // nothing we can do
	    }
	    if (connection != null) {
		try {
		    connection.close();
		} catch (SQLException e) {
		} // nothing we can do
	    }
	}
    }
}

[edit] What Result You Can Get

Coming soon...

[edit] Required External Libraries and/or Files for this Java Example

In order to run this Java example, one of the following libraries may be required: In order to run this example program, one of the following libraries may be required:


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools