Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.j256.ormlite.jdbc;
  
  
Implementation of the ConnectionSource interface that supports what is needed by ORMLite. This is not thread-safe nor synchronized. For other dataSources, see the DataSourceConnectionSource class.

NOTE: If you are using the Spring type wiring in Java, initialize() should be called after all of the set methods. In Spring XML, init-method="initialize" should be used.

Author(s):
graywatson
 
 public class JdbcConnectionSource extends BaseConnectionSource implements ConnectionSource {
 
 	private static Logger logger = LoggerFactory.getLogger(JdbcConnectionSource.class);
 
 	private String url;
 	private String username;
 	private String password;
 	protected boolean initialized = false;

Constructor for Spring type wiring if you are using the set methods. If you are using Spring then your should use: init-method="initialize"
 
 	public JdbcConnectionSource() {
 		// for spring type wiring
 	}

Create a data source for a particular database URL.

Parameters:
url The database URL which should start jdbc:...
Throws:
java.sql.SQLException If the driver associated with the database driver is not found in the classpath.
 
 	public JdbcConnectionSource(String urlthrows SQLException {
 		this(urlnullnullnull);
 	}

Create a data source for a particular database URL. The databaseType is usually determined from the databaseUrl so most users should call JdbcConnectionSource(java.lang.String) instead. If, however, you need to force the class to use a specific DatabaseType then this constructor should be used.

Parameters:
url The database URL which should start jdbc:...
databaseType Database to associate with this connection source.
Throws:
java.sql.SQLException If the driver associated with the database driver is not found in the classpath.
 
 	public JdbcConnectionSource(String urlDatabaseType databaseTypethrows SQLException {
 		this(urlnullnulldatabaseType);
 	}

Create a data source for a particular database URL with username and password permissions.

Parameters:
url The database URL which should start jdbc:...
username Username for permissions on the database.
password Password for permissions on the database.
Throws:
java.sql.SQLException If the driver associated with the database driver is not found in the classpath.
 
 	public JdbcConnectionSource(String urlString usernameString passwordthrows SQLException {
 		this(urlusernamepasswordnull);
 	}

Create a data source for a particular database URL with username and password permissions. The databaseType is usually determined from the databaseUrl so most users should call JdbcConnectionSource(java.lang.String,java.lang.String,java.lang.String) instead. If, however, you need to force the class to use a specific DatabaseType then this constructor should be used.

Parameters:
url The database URL which should start jdbc:...
username Username for permissions on the database.
password Password for permissions on the database.
databaseType Database to associate with this connection source.
Throws:
java.sql.SQLException If the driver associated with the database driver is not found in the classpath.
	public JdbcConnectionSource(String urlString usernameString passwordDatabaseType databaseType)
			throws SQLException {
		this. = url;
		this. = username;
		this. = password;
		this. = databaseType;
	}

Initialize the class after the setters have been called. If you are using the no-arg constructor and Spring type wiring, this should be called after all of the set methods.

Throws:
java.sql.SQLException If the driver associated with the database URL is not found in the classpath.
	public void initialize() throws SQLException {
		if () {
			return;
		}
		if ( == null) {
			throw new SQLException("url was never set on " + getClass().getSimpleName());
		}
		if ( == null) {
			 = DatabaseTypeUtils.createDatabaseType();
		}
		 = true;
	}
	public void close() throws SQLException {
		if (!) {
			throw new SQLException(getClass().getSimpleName() + " was not initialized properly");
		}
		if ( != null) {
			.debug("closed connection #{}".hashCode());
			 = null;
		}
	}
	public String getUrl() {
		return ;
	}
	public void setUrl(String url) {
		this. = url;
	}
		if (!) {
			throw new SQLException(getClass().getSimpleName() + " was not initialized properly");
		}
	}
		if (!) {
			throw new SQLException(getClass().getSimpleName() + " was not initialized properly");
		}
		if ( != null) {
				throw new SQLException("Connection has already been closed");
else {
				return ;
			}
		}
		return ;
	}
	public void releaseConnection(DatabaseConnection connectionthrows SQLException {
		if (!) {
			throw new SQLException(getClass().getSimpleName() + " was not initialized properly");
		}
		// noop right now
	}
	public boolean saveSpecialConnection(DatabaseConnection connectionthrows SQLException {
		// noop since this is a single connection source
		return true;
	}
	public void clearSpecialConnection(DatabaseConnection connection) {
		// noop since this is a single connection source
	}
		if (!) {
			throw new IllegalStateException(getClass().getSimpleName() + " was not initialized properly");
		}
		return ;
	}
	public boolean isOpen() {
		return  != null;
	}
	// not required
	public void setUsername(String username) {
		this. = username;
	}
	// not required
	public void setPassword(String password) {
		this. = password;
	}
	// not required
	public void setDatabaseType(DatabaseType databaseType) {
		this. = databaseType;
	}

Make a connection to the database.
		Properties properties = new Properties();
		if ( != null) {
			properties.setProperty("user");
		}
		if ( != null) {
			properties.setProperty("password");
		}
		JdbcDatabaseConnection connection = new JdbcDatabaseConnection(DriverManager.getConnection(properties));
		logger.debug("opened connection to {} got #{}"connection.hashCode());
		return connection;
	}
New to GrepCode? Check out our FAQ X