JdbcDataSource

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.

Methods
Connection getConnection()
Open a new connection using the current URL, user name and password.
Connection getConnection(String user, String password)
Open a new connection using the current URL and the specified user name and password.
PrintWriter getLogWriter()
Get the current log writer for this object.
int getLoginTimeout()
Get the login timeout in seconds, 0 meaning no timeout.
String getPassword()
Get the current password.
PooledConnection getPooledConnection()
Open a new pooled connection using the current URL, user name and password.
PooledConnection getPooledConnection(String user, String password)
Open a new pooled connection using the current URL and the specified user name and password.
Reference getReference()
Get a new reference for this object, using the current settings.
String getURL()
Get the current URL.
String getUser()
Get the current user name.
XAConnection getXAConnection()
Open a new XA connection using the current URL, user name and password.
XAConnection getXAConnection(String user, String password)
Open a new XA connection using the current URL and the specified user name and password.
void setLogWriter(PrintWriter out)
Set the current log writer for this object.
void setLoginTimeout(int timeout)
Set the login timeout in seconds, 0 meaning no timeout.
void setPassword(String password)
Set the current password.
void setPasswordChars(char[] password)
Set the current password in the form of a char array.
void setURL(String url)
Set the current URL.
void setUser(String user)
Set the current user name.

Connection getConnection() throws SQLException

Open a new connection using the current URL, user name and password.

Returns:
the connection

Connection getConnection(String user, String password) throws SQLException

Open a new connection using the current URL and the specified user name and password.

Parameters:
user - the user name
password - the password
Returns:
the connection

PrintWriter getLogWriter()

Get the current log writer for this object.

Returns:
the log writer

int getLoginTimeout()

Get the login timeout in seconds, 0 meaning no timeout.

Returns:
the timeout in seconds

String getPassword()

Get the current password.

Returns:
the password

PooledConnection getPooledConnection() throws SQLException

Open a new pooled connection using the current URL, user name and password.

Returns:
the connection

PooledConnection getPooledConnection(String user, String password) throws SQLException

Open a new pooled connection using the current URL and the specified user name and password.

Parameters:
user - the user name
password - the password
Returns:
the connection

Reference getReference()

Get a new reference for this object, using the current settings.

Returns:
the new reference

String getURL()

Get the current URL.

Returns:
the URL

String getUser()

Get the current user name.

Returns:
the user name

XAConnection getXAConnection() throws SQLException

Open a new XA connection using the current URL, user name and password.

Returns:
the connection

XAConnection getXAConnection(String user, String password) throws SQLException

Open a new XA connection using the current URL and the specified user name and password.

Parameters:
user - the user name
password - the password
Returns:
the connection

void setLogWriter(PrintWriter out)

Set the current log writer for this object. This value is ignored by this database.

Parameters:
out - the log writer

void setLoginTimeout(int timeout)

Set the login timeout in seconds, 0 meaning no timeout. The default value is 0. This value is ignored by this database.

Parameters:
timeout - the timeout in seconds

void setPassword(String password)

Set the current password.

Parameters:
password - the new password.

void setPasswordChars(char[] password)

Set the current password in the form of a char array.

Parameters:
password - the new password in the form of a char array.

void setURL(String url)

Set the current URL.

Parameters:
url - the new URL

void setUser(String user)

Set the current user name.

Parameters:
user - the new user name