public class JdbcDataSource extends TraceObject implements XADataSource, DataSource, ConnectionPoolDataSource, Serializable, Referenceable
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.ARRAY, BLOB, CALLABLE_STATEMENT, CLOB, CONNECTION, DATA_SOURCE, DATABASE_META_DATA, PARAMETER_META_DATA, PREPARED_STATEMENT, RESULT_SET, RESULT_SET_META_DATA, SAVEPOINT, SQL_EXCEPTION, STATEMENT, trace, XA_DATA_SOURCE, XA_RESOURCE, XID| Constructor and Description |
|---|
JdbcDataSource()
The public constructor.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
String |
getDescription()
Get the current description.
|
int |
getLoginTimeout()
Get the login timeout in seconds, 0 meaning no timeout.
|
PrintWriter |
getLogWriter()
Get the current log writer for this object.
|
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 |
setDescription(String description)
Set the description.
|
void |
setLoginTimeout(int timeout)
Set the login timeout in seconds, 0 meaning no timeout.
|
void |
setLogWriter(PrintWriter out)
Set the current log writer for this object.
|
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.
|
String |
toString()
INTERNAL
|
debugCode, debugCodeAssign, debugCodeCall, debugCodeCall, debugCodeCall, getNextId, getTrace, getTraceId, getTraceObjectName, isDebugEnabled, isInfoEnabled, logAndConvert, quote, quoteArray, quoteBigDecimal, quoteBytes, quoteDate, quoteIntArray, quoteMap, quoteTime, quoteTimestamp, setTrace, unsupportedclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitisWrapperFor, unwrapgetParentLoggerpublic int getLoginTimeout()
getLoginTimeout in interface CommonDataSourcepublic void setLoginTimeout(int timeout)
setLoginTimeout in interface CommonDataSourcetimeout - the timeout in secondspublic PrintWriter getLogWriter()
getLogWriter in interface CommonDataSourcepublic void setLogWriter(PrintWriter out)
setLogWriter in interface CommonDataSourceout - the log writerpublic Connection getConnection() throws SQLException
getConnection in interface DataSourceSQLExceptionpublic Connection getConnection(String user, String password) throws SQLException
getConnection in interface DataSourceuser - the user namepassword - the passwordSQLExceptionpublic String getURL()
public void setURL(String url)
url - the new URLpublic void setPassword(String password)
password - the new password.public void setPasswordChars(char[] password)
password - the new password in the form of a char array.public String getPassword()
public String getUser()
public void setUser(String user)
user - the new user namepublic String getDescription()
public void setDescription(String description)
description - the new descriptionpublic Reference getReference()
getReference in interface Referenceablepublic XAConnection getXAConnection() throws SQLException
getXAConnection in interface XADataSourceSQLExceptionpublic XAConnection getXAConnection(String user, String password) throws SQLException
getXAConnection in interface XADataSourceuser - the user namepassword - the passwordSQLExceptionpublic PooledConnection getPooledConnection() throws SQLException
getPooledConnection in interface ConnectionPoolDataSourceSQLExceptionpublic PooledConnection getPooledConnection(String user, String password) throws SQLException
getPooledConnection in interface ConnectionPoolDataSourceuser - the user namepassword - the passwordSQLExceptionCopyright © 2012 JBoss by Red Hat. All Rights Reserved.