FileDocCategorySizeDatePackage
UserFactory.javaAPI DocExample4738Sat Jan 24 10:44:40 GMT 2004je3.servlet

UserFactory

public class UserFactory extends Object
A class for creating new users in a database and retreiving existing users from the database. Note that it assumes that a database table with the specified name exists. You can create such a table with SQL like this: CREATE TABLE subscribers ( email VARCHAR(64) PRIMARY KEY, password VARCHAR(20), html BIT, digest BIT);

Fields Summary
Connection
db
String
tablename
PreparedStatement
insertUser
PreparedStatement
selectUser
PreparedStatement
updateUser
PreparedStatement
deleteUser
Constructors Summary
public UserFactory(Connection db, String tablename)

	this.db = db;
	this.tablename = tablename;

	// Prepare the SQL statements we'll use later.  Parameters will be 
	// subsituted for the question marks in the methods below.
	insertUser = db.prepareStatement("insert into " + tablename + 
					 "(email,password,html,digest) " +
					 "values(?,?,0,0)");
	selectUser = db.prepareStatement("select * from " + tablename +
					 " where email=?");
	deleteUser = db.prepareStatement("delete from " + tablename +
					 " where email=?");
	updateUser = db.prepareStatement("update " + tablename +
					 " set html=?,digest=? where email=?");
    
Methods Summary
public voiddelete(User user)

	if (user.deleted) return;  // make sure we're not already deleted
	// Delete the user from the database
	deleteUser.setString(1, user.getEmailAddress());
	deleteUser.executeUpdate();
	user.deleted = true;  // Don't allow update() after delete()
    
public Userinsert(java.lang.String email, java.lang.String password)

	// Check whether the user already exists
	selectUser.setString(1, email);
	ResultSet results = selectUser.executeQuery();
	if (results.next()) throw new UserAlreadyExists(email);

	// If not, create a new entry in the database
	insertUser.setString(1, email);
	insertUser.setString(2, password);
	insertUser.executeUpdate();
	
	// And return a matching User object to the caller
	return new User(email, false, false);
    
public Userselect(java.lang.String email, java.lang.String password)

	// Look up the user
	selectUser.setString(1, email);
	ResultSet results = selectUser.executeQuery();
	// Check that the user exists
	if (!results.next()) throw new NoSuchUser(email);
	// Check that the password is correct
	String pw = results.getString("password");
	if (!pw.equals(password)) throw new BadPassword(email);
	// Return a User object representing this user and their mail prefs.
	boolean html = results.getInt("html") == 1;
	boolean digest = results.getInt("digest") == 1;
	return new User(email, html, digest);
    
public voidupdate(User user)

	if (user.deleted) return;  // Don't allow updates to deleted users
	// Update the database record to reflect new preferences
	updateUser.setInt(1, user.getPrefersHTML()?1:0);
	updateUser.setInt(2, user.getPrefersDigests()?1:0);
	updateUser.setString(3, user.getEmailAddress());
	updateUser.executeUpdate();