I have a basic CRUD DAO using JDBC to access my database. I using a connection pool to get a connection in each method and then execute my commands. In some scenarios for update User
, there might be cases where I don't have to update the whole DTO to the database. In my insert and my findbykey()
methods, I found out quickly how complicated it gets. I'm thinking either my design of my DTO and DAO are poorly constructed or my logic for my code is poor.
Any help in either readability or redesign?
public class SQLUserDAO implements GenericDAO<User, String, Boolean>
{
@Override
public void update(User user, Boolean active) throws NotFoundException
{
// Create the ConnectionPool:
JDBCConnectionPool pool = JDBCConnectionPool.getPoolInstance();
// Get a connection:
Connection con = pool.checkOut();
// Return the connection:
pool.checkIn(con);
}
@Override
public void delete(User user, Boolean active) throws NotFoundException
{
// Create the ConnectionPool:
JDBCConnectionPool pool = JDBCConnectionPool.getPoolInstance();
// Get a connection:
Connection con = pool.checkOut();
// Return the connection:
pool.checkIn(con);
}
@Override
public User findByKey(String key, Boolean active) throws NotFoundException
{
DefaultUser tempUser = null;
// Create the ConnectionPool:
JDBCConnectionPool pool = JDBCConnectionPool.getPoolInstance();
// Get a connection:
Connection con = pool.checkOut();
String query = "SELECT * FROM users WHERE user_name ='" + key + "'";
try
{
PreparedStatement stmt = con.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
String user_name = null, user_id = null, user_password = null, user_type = null, first_name = null, last_name = null, creation_date = null;
while (rs.next())
{
user_name = (rs.getString(1));
user_id = rs.getString(2);
user_password = (rs.getString(3));
user_type = (rs.getString(4));
first_name = (rs.getString(5));
last_name = (rs.getString(6));
creation_date = (rs.getString(7));
}
/*
* LOG.CONSOLE.debug(user_name); LOG.CONSOLE.debug(user_id);
* LOG.CONSOLE.debug(user_password);
* LOG.CONSOLE.debug(user_type);
* LOG.CONSOLE.debug(first_name);
* LOG.CONSOLE.debug(last_name);
* LOG.CONSOLE.debug(creation_date);
*/
String[] userTypeString = user_type.split("\\+");
List<UserType> userType = new ArrayList<UserType>();
if (userTypeString != null)
{
for (String useType : userTypeString)
{
char[] userTypeCharArray = useType
.toCharArray();
switch (userTypeCharArray[0])
{
case 'A' :
userType.add(UserType.ADMIN);
break;
case 'S' :
userType.add(UserType.SHAREHOLDER);
break;
case 'B' :
userType.add(UserType.BROKER);
break;
}
}
}
Date date = new SimpleDateFormat("dy mon d hh24:mi:ss yyyy")
.parse(creation_date);
tempUser = new DefaultUser(user_name,
UUID.fromString(user_id), user_password, userType,
first_name, last_name, date);
}
catch (SQLException e)
{
pool.checkIn(con);
e.printStackTrace();
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
pool.checkIn(con);
return tempUser;
}
@Override
public User findByValue(User object, Boolean active)
throws NotFoundException
{
// Create the ConnectionPool:
JDBCConnectionPool pool = JDBCConnectionPool.getPoolInstance();
// Get a connection:
Connection con = pool.checkOut();
// Return the connection:
pool.checkIn(con);
return null;
}
@Override
public void insert(User user, Boolean active) throws NotFoundException
{
// Create the ConnectionPool:
JDBCConnectionPool pool = JDBCConnectionPool.getPoolInstance();
// Get a connection:
Connection conn = pool.checkOut();
try
{
PreparedStatement preparedStatement;
preparedStatement = pool
.checkOut()
.prepareStatement(
"INSERT INTO users(user_name,user_id,user_password,creation_date,first_name,last_name,user_type)"
+ "VALUES (?,?,?,?,?,?,?)");
preparedStatement.setString(1, user.getUserName());
preparedStatement.setString(2, user.getUserId().toString());
preparedStatement.setString(3, user.getClearTextPassword());
preparedStatement.setString(4, user.getCreationDate()
.toString());
preparedStatement.setString(5, user.getFirstName());
preparedStatement.setString(6, user.getLastName());
StringBuilder userTypeCode = new StringBuilder();
if (user.getUserTypes() != null)
{
for (UserType useType : user.getUserTypes())
{
switch (useType)
{
case ADMIN :
userTypeCode.append(UserType.ADMIN
.toString());
break;
case SHAREHOLDER :
userTypeCode
.append(UserType.SHAREHOLDER
.toString());
break;
case BROKER :
userTypeCode.append(UserType.BROKER
.toString());
break;
}
userTypeCode.append("+");
}
}
preparedStatement.setString(7, userTypeCode.toString());
preparedStatement.executeUpdate();
}
catch (SQLException e)
{
pool.checkIn(conn);
e.printStackTrace();
}
// Return the connection:
pool.checkIn(conn);
}
}