All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.katujo.web.utils.DatabaseManager Maven / Gradle / Ivy

Go to download

Util classes for a Java web application with a SQL database back end that communicates with clients using JSON.

The newest version!

//Namespace
package com.katujo.web.utils;

//Java imports
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;


//Google imports
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * Helps in database communication. 
 * @author Johan Hertz
 *
 */
public class DatabaseManager
{	
	//This map holds the data sources
	private final static Map dataSources = new ConcurrentHashMap();
	
	//The default data source look up that is used when calling methods without the data source specified
	private final String defaultLookup;	
	
	/**
	 * Create the object without a default data source look up set.
	 *
	 */
	public DatabaseManager() 
	{
		this.defaultLookup = null;
	}
	
	/**
	 * Create the object with the default data source look up set.
	 * @param defaultDataSource
	 */
	public DatabaseManager(String defaultDataSource)
	{
		this.defaultLookup = defaultDataSource;
	}
	
	/**
	 * Get the default data source.
	 * @return
	 * @throws Exception
	 */
	protected DataSource getDataSource() throws Exception
	{
		return getDataSource(defaultLookup);
	}
			
	/**
	 * Get the data source using the JNDI name.
	 * 

* On Tomcat this should be java:comp/env/DATA_SOURCE_NAME *

* @param name * @return * @throws Exception */ protected DataSource getDataSource(String lookup) throws Exception { //Try to get the data source try { //Get the data source from the map DataSource source = dataSources.get(lookup); //Create and add the data source if not set if(source == null) { //Get the context objects Context initCtx = new InitialContext(); source = (DataSource) initCtx.lookup(lookup); //Add the source to the data sources dataSources.put(lookup, source); } //Return the source return source; } //Failed catch(Exception ex) { throw new Exception("Failed to get the data source with lookup " + lookup, ex); } } /** * Get a connection from the default data source. *

* This connection must be explicitly closed by the caller. *

* @return * @throws Exception */ public Connection getConnection() throws Exception { //Try to get a connection try {return getDataSource(defaultLookup).getConnection();} //Failed catch(Exception ex) { throw new Exception("Failed to get a connection", ex); } } /** * Get a connection from the named data source. *

* This connection must be explicitly closed by the caller. *

* @param name * @return * @throws Exception */ public Connection getConnection(String name) throws Exception { //Try to get a connection try {return getDataSource(name).getConnection();} //Failed catch(Exception ex) { throw new Exception("Failed to get a connection", ex); } } /** * Load a JSON object from the database using the SQL. *

* If no result matched the query null will be returned. *

* @param sql * @return * @throws Exception */ protected JsonObject getObject(String sql) throws Exception { return getObject(sql, new Object[]{}); } /** * Load a JSON object from the database using the SQL and the parameter. *

* If no result matched the query null will be returned. *

* @param sql * @param parameter * @return * @throws Exception */ protected JsonObject getObject(String sql, Object parameter) throws Exception { return getObject(sql, new Object[]{parameter}); } /** * Load a JSON object from the database using the SQL and the parameters. *

* If no result matched the query null will be returned. *

* @param sql * @param parameters * @return * @throws Exception */ protected JsonObject getObject(String sql, Object[] parameters) throws Exception { //Fields Connection connection = null; PreparedStatement statement = null; ResultSet result = null; //Try to read data try { //Get a connection connection = getConnection(); //Create the statement statement = connection.prepareStatement(sql); //Set the parameters if set if(parameters != null) for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy