A data source for H2 database connections. It is a factory for XAConnection and Connection objects. This class is usually registered in a JNDI naming service. To create a data source object and register it with a JNDI service, use the following code:
import org.h2.jdbcx.JdbcDataSource; import javax.naming.Context; import javax.naming.InitialContext; JdbcDataSource ds = new JdbcDataSource(); ds.setURL("jdbc:h2:˜/test"); ds.setUser("sa"); ds.setPassword("sa"); Context ctx = new InitialContext(); ctx.bind("jdbc/dsName", ds);
To use a data source that is already registered, use the following code:
import java.sql.Connection; import javax.sql.DataSource; import javax.naming.Context; import javax.naming.InitialContext; Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("jdbc/dsName"); Connection conn = ds.getConnection();
In this example the user name and password are serialized as well; this may be a security problem in some cases.