Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
AnalyzerBeans Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 
 package org.eobjects.analyzer.storage;
 
 import java.util.List;
 
 class SqlDatabaseList<E> extends AbstractList<E> implements List<E>, SqlDatabaseCollection {
 
 	private final Connection _connection;
 	private final String _tableName;
 	private volatile int _size;
 
 	public SqlDatabaseList(Connection connectionString tableNameString valueTypeName) {
 		 = connection;
 		 = tableName;
 		 = 0;
 
 				+ " (list_index INTEGER PRIMARY KEY, list_value " + valueTypeName + ")");
 	}
 
 	public synchronized E remove(int index) {
 		E oldValue = get(index);
 		SqlDatabaseUtils.performUpdate("DELETE FROM " +  + " WHERE list_index=" + index);
 		SqlDatabaseUtils.performUpdate("UPDATE " + 
 				+ " SET list_index = list_index-1 WHERE list_index > " + index);
 		--;
 		return oldValue;
 	}
 
 	public synchronized void clear() {
 		SqlDatabaseUtils.performUpdate("DELETE FROM " + );
 		 = 0;
 	}
 
 	public E get(int index) {
 		Statement st = null;
 		ResultSet rs = null;
 		try {
 			rs = st.executeQuery("SELECT list_value FROM " +  + " WHERE list_index=" + index + ";");
 			if (rs.next()) {
 				@SuppressWarnings("unchecked")
 				E result = (E) rs.getObject(1);
 				if (rs.wasNull()) {
 					return null;
 				}
 				return result;
 			}
 			throw new IndexOutOfBoundsException("No such index: " + index);
 		} catch (SQLException e) {
 			throw new IllegalStateException(e);
 		} finally {
 			SqlDatabaseUtils.safeClose(rsst);
 		}
 	}
 
 	public int size() {
 		return ;
 	}
 
 	public synchronized boolean add(E elem) {
 		PreparedStatement st = null;
 		try {
 			if ( == 0) {
 				// first time is different
 				st = .prepareStatement("INSERT INTO " +  + " VALUES(0, ?)");
 			} else {
 				st = .prepareStatement("INSERT INTO " +  + " VALUES((SELECT MAX(list_index)+1 FROM "
 						+  + "), ?)");
 			}
 			st.setObject(1, elem);
			st.execute();
catch (SQLException e) {
			throw new IllegalStateException(e);
finally {
			SqlDatabaseUtils.safeClose(nullst);
		}
		return true;
	}
	public synchronized void add(int index, E element) {
		SqlDatabaseUtils.performUpdate("UPDATE " + 
" SET list_index = list_index+1 WHERE list_index > " + index);
		PreparedStatement st = null;
		try {
			st = .prepareStatement("INSERT INTO " +  + " VALUES(?, ?)");
			st.setObject(1, index);
			st.setObject(2, element);
catch (SQLException e) {
			throw new IllegalStateException(e);
finally {
			SqlDatabaseUtils.safeClose(nullst);
		}
	};
	public synchronized E set(int index, E element) {
oldValue = get(index);
		PreparedStatement st = null;
		try {
			st = .prepareStatement("UPDATE " +  + " SET list_value=? WHERE list_index=?");
			st.setObject(1, element);
			st.setObject(2, index);
catch (SQLException e) {
			throw new IllegalStateException(e);
finally {
			SqlDatabaseUtils.safeClose(nullst);
		}
		return oldValue;
	};
	public String getTableName() {
		return ;
	};
	protected void finalize() throws Throwable {
		super.finalize();
		SqlDatabaseUtils.performUpdate("DROP TABLE " + getTableName());
	}
New to GrepCode? Check out our FAQ X