PubHub Code Sample:

Java (DAO Interface)

//Pre-implementation of the Tag DAO

package examples.pubhub.dao;

import java.util.List;

import examples.pubhub.model.Book;
import examples.pubhub.model.Tag;

public interface TagDAO {

	public boolean addTag(Tag tag);
	public boolean addTag(String tagName);
	
	public boolean removeTag(String name);
	
	public boolean attachTagToBook(String tagName, String isbn_13);
	
	public boolean removeTagRelationship(String tagName, String isbn_13);
	
	// -------------
	
	public List<Tag> getAllTags();
	
	public List<String> getAllRelationships();
	
	public List<Tag> getTagsOfBook(String bookISBN);
	
	public Tag getTag(String tagName);
}

Java (DAO Implementation)

package examples.pubhub.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import examples.pubhub.model.Book;
import examples.pubhub.model.Tag;
import examples.pubhub.utilities.DAOUtilities;

public class TagDAOImpl implements TagDAO {

	Connection connection = null;	// Our connection to the database
	PreparedStatement sqlStatement = null;	// We use prepared statements to help protect against SQL injection
	List<Tag> testList = new ArrayList<Tag>();
	List<Book> testBookList = new ArrayList<Book>();
	
	//----------------------------------------------
	
	@Override
	public boolean addTag(Tag tag)
	{
		try {
			connection = DAOUtilities.getConnection(); //establishes connection to pubhub
			String sql = "INSERT INTO tags (tagname) VALUES (?)";
			sqlStatement = connection.prepareStatement(sql);
			
			//sqlStatement.setInt(1, tag.getTagID());
			sqlStatement.setString(1, tag.getTagName());
			
			
			//checks to see if the above statement executed
			if (sqlStatement.executeUpdate() != 0)
			{
				//if it was successfully updated
				return true;
			}	
			else
				return false;
			
		} catch (SQLException exception) {
			exception.printStackTrace();
			return false;
		} finally {
			closeResources();
		}
	}

//-----
// Many other definitions were ommitted from this example...
//-----

public Tag getTag(String tagName)
	{
		Tag tag = new Tag();
		//tag = null;
		
		try {
			connection = DAOUtilities.getConnection();	// Get our database connection from the manager
			String sql = "SELECT * FROM tags WHERE tagname = ?";			// Our SQL query
			sqlStatement = connection.prepareStatement(sql);	// Creates the prepared statement from the query
			
			sqlStatement.setString(1, tagName);
			
			ResultSet resultSet = sqlStatement.executeQuery();
			
			//should only contain one element but you know
			if (resultSet.next()) {
				tag.setTagName(resultSet.getString("tagname"));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// We need to make sure our statements and connections are closed, 
			// or else we could wind up with a memory leak
			closeResources();
		}
		
		return tag;
	}
	
	//closes resources 
	private void closeResources() {
			try {
				if (sqlStatement != null)
					sqlStatement.close();
			} catch (SQLException e) {
				System.out.println("Could not close statement!");
				e.printStackTrace();
			}
			
			try {
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				System.out.println("Could not close connection!");
				e.printStackTrace();
			}
		}
}