|
|
-
/* Copyright (c) 1995 Sun Microsystems, Inc. All Rights reserved
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file copyright.html
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
- /**
- * JdbcImplementation of the UserRegistry.
- * Table created as follows:
- * create table UTH(
- * userid varchar(50) UNIQUE,
- * password varchar (50),
- * email varchar (50)
- * ) tablespace register
- * @author Tony Squier
- * @version 1.2 97/01/14 09:49:30
- */
- import java.util.*;
- import java.sql.*;
- import jdc.database.*;
- public class UserRegistryJdbcImpl implements UserRegistry
- {
- /**
- * Name of Table entries are queried from.
- */
- final static private String _driver = "sun.jdbc.odbc.JdbcOdbcDriver";
- final static private String _user = "scott";
- final static private String _pass = "tiger";
- final static private String _url = "jdbc:odbc:jdc";
- /**
- * Database Connection.
- */
- static private Properties _dbProperties = null;
- static private Driver _jdbcDriver = null;
- static private Connection _db = null;
- /**
- * Insert and Update statements
- */
- static private PreparedStatement _insertStatement;
- static private PreparedStatement _updateStatement;
- static private PreparedStatement _queryStatement;
- /**
- * Initialize driver. We do this hear so that we only have
- * one driver to use for all instances of this class.
- */
- static
- {
- try
- {
- _jdbcDriver = (Driver)Class.forName (_driver).newInstance();
- _dbProperties = new Properties();
- _dbProperties.put ("user", _user);
- _dbProperties.put ("password", _pass);
- _init();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- /**
- * Returns an array of found JDCMembers
- * @param userId Search text.
- * @return JDCMember[] Array JDCMembers instances found in the database.
- * @throws JDCAdminException Error occured in database.
- */
- public JDCMember[] getMember (String userId) throws
JDCAdminException
- {
- Vector members = new Vector();
- String sql;
- ResultSet r = null;
- Statement statement = null;
- synchronized (_jdbcDriver)
- {
- try
- {
- // Check to see if the database connection
- // is still open, if not re-open.
- if ( _db.isClosed () )
- {
- _init();
- }
- // Now, lets do the query.
- JDCMember member;
- _queryStatement.setString (1, userId);
- r = _queryStatement.executeQuery();
- while ( r.next() )
- {
- member = new JDCMember ();
- member.setUserId (r.getString ("USERID"));
- member.setPassword (r.getString ("PASSWORD"));
- member.setEmail (r.getString ("EMAIL"));
- member.clearChanged();
- members.addElement (member);
- }
- }
- catch (SQLException e)
- {
- throw new JDCAdminException (e.getMessage());
- }
- finally
- {
- try
- {
- _queryStatement.clearParameters();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- try
- {
- if ( r != null )
- {
- r.close();
- }
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- JDCMember memberArray[] = new JDCMember[members.size()];
- members.copyInto (memberArray);
- return memberArray;
- }
- /**
- * Adds a member to the database.
- * @param member The JDCMember to add/replace.
- * @return JDCMember The original value of the member or null if
- * it didn't exist.
- * @throws JDCAdminException
- */
- public JDCMember setMember (JDCMember member) throws
JDCAdminException
- {
- JDCMember hold[];
- JDCMember oldMember = null;
- ResultSet results = null;
- PreparedStatement statement = null;
- // See if member exists
- hold = getMember (member.getUserId());
- synchronized (_jdbcDriver)
- {
- try
- {
- // Check to see if the database connection
- // is still open, if not re-open.
- if ( _db.isClosed () )
- {
- _init();
- }
- int resultCode;
- // We've found an existing member, so keep it because
- // we return the old value. Also, we must do an update
- // not an insert.
- if ( hold.length == 1 )
- {
- oldMember = hold[0];
- _updateStatement.setString (1, member.getPassword());
- _updateStatement.setString (2, member.getEmail());
- _updateStatement.setString (3, member.getUserId());
- statement = _updateStatement;
- resultCode = statement.executeUpdate();
- }
- // No existing member, so do an insert.
- else if ( hold.length == 0 )
- {
- _insertStatement.setString (1, member.getUserId());
- _insertStatement.setString (2, member.getPassword());
- _insertStatement.setString (3, member.getEmail());
- statement = _insertStatement;
- resultCode = statement.executeUpdate ();
- }
- // More then one member with the given user id is in the
- // database - this should never happen if we are
- // using user id as a unqiue key.
- else
- {
- throw new JDCAdminException ("Duplicate UserId!");
- }
- // Problem occured while doing the update/insert.
- if ( resultCode == -1 )
- {
- throw new JDCAdminException ("Result Code: " + resultCode);
- }
- }
- catch (SQLException e)
- {
- String s = e.getMessage();
- throw new JDCAdminException (e.getMessage());
- }
- finally
- {
- if ( statement != null )
- {
- try
- {
- statement.clearParameters();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- return oldMember;
- }
- /**
- * Convience method to initalize the database connection
- * and create the Prepared statements.
- * @return void
- */
- static private void _init()
- {
- synchronized (_jdbcDriver)
-
{
- try
- {
- _db = _jdbcDriver.connect (_url, _dbProperties);
- _insertStatement = _db.prepareStatement ("insert into UTH (userid,password,email)
values (?,?,?)");
- _updateStatement = _db.prepareStatement ("update UTH set password=?,email=?
where userid=?");
- _queryStatement = _db.prepareStatement ("select * from UTH where userid=?");
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
|
|