|
|
package neuralbuildsample.rdbms;
import neuralbuildsample.dbutil.*;
import neuralbuildsample.exceptions.NeuralBuildJavaSampleDAODeleteException; import neuralbuildsample.exceptions.NeuralBuildJavaSampleDAOFindException; import neuralbuildsample.exceptions.NeuralBuildJavaSampleDAOInsertException; import neuralbuildsample.exceptions.NeuralBuildJavaSampleDAOUpdateException;
import neuralbuildsample.valueobjects.Company;
import sun.jdbc.rowset.CachedRowSet;
import java.sql.Connection;
/** * * Relational Data Access implementation for NeuralBuildJavaSample. * */ /** * * <copyright> * Copyright (c) 2006 NeuralLimits * </copyright> */ import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.util.ArrayList; import java.util.Collection;
public class RDBMSCompanyDAO implements neuralbuildsample.daoaccess.CompanyDAO { /** * Use JDBC to obtain a connection to the database. */ public static final int JDBC = 1;
/** * Use a JNDI lookup to obtain the database connection from a * pool. */ public static final int JNDI = 2; public DBConnectionManagerJDBC connMgr; private Connection con = null;
/** * Initialise the data access object. */ public RDBMSCompanyDAO() { connMgr = DBConnectionManagerJDBC.getInstance(); }
/** * Insert a new Company into the data source. * @param criteriaCompany * The object to be inserted into the database. * @return The primary key value assigned by the implementation. * @throws NeuralBuildJavaSampleDAOInsertException If the object cannot be added to the data source. */ public Integer insertCompany(Company criteriaCompany) throws NeuralBuildJavaSampleDAOInsertException { Integer keyVal = new Integer(0); con = connMgr.getConnection("neuralbuildjavasample");
int setindex = 1; String vSQL = constructInsertPreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL, PreparedStatement.RETURN_GENERATED_KEYS);
if (criteriaCompany.CompanyID_isSet) { ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; }
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) { keyVal = Integer.valueOf(rs.getObject(1).toString()); }
rs.close(); rs = null; ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOInsertException("Insert Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return keyVal; }
/** * Delete an object from the database. * @param criteriaCompany * The object to be deleted from the database. * The object is identified by the primary key value, which should be set * in newCompany. * @return true if the object was deleted. * @throws NeuralBuildJavaSampleDAODeleteException if the object cannot be deleted. */ public boolean deleteCompany(Company criteriaCompany) throws NeuralBuildJavaSampleDAODeleteException { boolean deletestatus = true; con = connMgr.getConnection("neuralbuildjavasample");
int setindex = 1; String vSQL = constructDeletePreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL);
if (criteriaCompany.CompanyID_isSet) { ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; }
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
ps.executeUpdate(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAODeleteException("Delete Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return deletestatus; }
/** * Update an object in the database. * @param criteriaCompany * The object to be updated. * The object is identified by the primary key value, which should be set * in criteriaCompany. * @return true if the object was updated. * @throws NeuralBuildJavaSampleDAOUpdateException if the object cannot be updated. */ public boolean updateCompany(Company criteriaCompany) throws NeuralBuildJavaSampleDAOUpdateException { con = connMgr.getConnection("neuralbuildjavasample");
boolean updatestatus = true; int setindex = 1; String vSQL = constructUpdatePreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL);
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
//Handle Key Value ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; ps.executeUpdate(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOUpdateException("Update Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return updatestatus; }
/** * Find an object that matches the supplied field values. * @param criteriaCompany * The supplied criteria. * @return The located object, or null if the object cannot be found. * @throws NeuralBuildJavaSampleDAOFindException If a database error occurs. */ public Company findCompany(Company criteriaCompany) throws NeuralBuildJavaSampleDAOFindException { con = connMgr.getConnection("neuralbuildjavasample");
Company newCompany = new Company(); ResultSet rs = null; int setindex = 1; String vSQL = constructFindPreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
if (criteriaCompany.CompanyID_isSet) { ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; }
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
//Execute Query rs = ps.executeQuery();
//Populate Value Object if (rs.first()) { if (rs.getObject(1) != null) { newCompany.setCompanyID(Integer.valueOf(rs.getInt(1))); }
if (rs.getObject(2) != null) { newCompany.setCompanyName(rs.getDate(2)); } }
rs.close(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOFindException("Find Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return newCompany; }
/** * Find an object that matches the supplied field values. * @param criteriaCompany * The supplied criteria. * @return A rowset containing all the rows that match the supplied criteria. * @throws NeuralBuildJavaSampleDAOFindException If a database error occurs. */ public CachedRowSet selectCompanyRS(Company criteriaCompany) throws NeuralBuildJavaSampleDAOFindException { con = connMgr.getConnection("neuralbuildjavasample");
CachedRowSet Rs = null; ResultSet rs = null; int setindex = 1; String vSQL = constructFindPreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
if (criteriaCompany.CompanyID_isSet) { ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; }
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
//Execute Query rs = ps.executeQuery(); Rs = new CachedRowSet(); Rs.populate(rs); rs.close(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOFindException("Find Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return Rs; }
/** * Find a list of Company that match the supplied field values. * @param criteriaCompany * The supplied criteria. * @return A collection of Company containing the matching rows. * @throws NeuralBuildJavaSampleDAOFindException If a database error occurs. */ public Collection selectCompanyTO(Company criteriaCompany) throws NeuralBuildJavaSampleDAOFindException { con = connMgr.getConnection("neuralbuildjavasample");
Collection Col = new ArrayList(); ResultSet rs = null; int setindex = 1; String vSQL = constructFindPreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
if (criteriaCompany.CompanyID_isSet) { ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; }
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
//Execute Query rs = ps.executeQuery();
while (rs.next()) { Company newCompany = new Company();
if (rs.getObject(1) != null) { newCompany.setCompanyID(Integer.valueOf(rs.getInt(1))); }
if (rs.getObject(2) != null) { newCompany.setCompanyName(rs.getDate(2)); }
Col.add(newCompany); }
rs.close(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOFindException("Find Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return Col; }
/** * Find all the Company objects. * @return A collection of Company objects containing the matching rows. * @throws NeuralBuildJavaSampleDAOFindException If a database error occurs. */ public Collection selectCompanyALL() throws NeuralBuildJavaSampleDAOFindException { con = connMgr.getConnection("neuralbuildjavasample");
Collection Col = new ArrayList(); ResultSet rs = null; String vSQL = " SELECT * FROM tcompany";
try { PreparedStatement ps = con.prepareStatement(vSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//Execute Query rs = ps.executeQuery();
while (rs.next()) { Company newCompany = new Company();
if (rs.getObject(1) != null) { newCompany.setCompanyID(Integer.valueOf(rs.getInt(1))); }
if (rs.getObject(2) != null) { newCompany.setCompanyName(rs.getDate(2)); }
Col.add(newCompany); }
rs.close(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOFindException("Find Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return Col; }
/** * Find a list of Company that match the supplied field values. * @param criteriaCompany * The supplied criteria. * @return A collection of Company containing the matching rows. * @throws NeuralBuildJavaSampleDAOFindException If a database error occurs. */ public Collection selectCompanyLIKE(Company criteriaCompany) throws NeuralBuildJavaSampleDAOFindException { con = connMgr.getConnection("neuralbuildjavasample");
Collection Col = new ArrayList(); ResultSet rs = null; int setindex = 1; String vSQL = constructFindLIKEPreparedStatement(criteriaCompany);
try { PreparedStatement ps = con.prepareStatement(vSQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
if (criteriaCompany.CompanyID_isSet) { ps.setInt(setindex, criteriaCompany.getCompanyID().intValue()); setindex++; }
if (criteriaCompany.CompanyName_isSet) { ps.setDate(setindex, (java.sql.Date) criteriaCompany.getCompanyName()); setindex++; }
//Execute Query rs = ps.executeQuery();
while (rs.next()) { Company newCompany = new Company();
if (rs.getObject(1) != null) { newCompany.setCompanyID(Integer.valueOf(rs.getInt(1))); }
if (rs.getObject(2) != null) { newCompany.setCompanyName(rs.getDate(2)); }
Col.add(newCompany); }
rs.close(); ps.close(); } catch (Exception e) { connMgr.freeConnection("neuralbuildjavasample", con); throw new NeuralBuildJavaSampleDAOFindException("Find Failed " + e.getMessage(), e); }
connMgr.freeConnection("neuralbuildjavasample", con);
return Col; }
/** * Construct a find prepared statement for the criteria object. * @param criteriaCompany * The supplied criteria. * @return String containing the Prepared Statement. */ private String constructFindPreparedStatement(Company criteriaCompany) { String vSQL = "SELECT ";
boolean bhasSet = false;
int setindex = 1; String vWhereVals = " ";
vSQL = vSQL + "companyid"; vSQL = vSQL + ",companyname";
if (criteriaCompany.CompanyID_isSet) { if (bhasSet) { vWhereVals = vWhereVals + " AND "; }
vWhereVals = vWhereVals + " companyid=?"; bhasSet = true; }
if (criteriaCompany.CompanyName_isSet) { if (bhasSet) { vWhereVals = vWhereVals + " AND "; }
vWhereVals = vWhereVals + " companyname=?"; bhasSet = true; }
vSQL = vSQL + " FROM tcompany ";
if (setindex > 0) { vSQL = vSQL + " WHERE "; }
vSQL = vSQL + vWhereVals;
return vSQL; }
/** * Construct a find LIKE prepared statement for the criteria object. * @param criteriaCompany. The supplied criteria. * @return String containing the Prepared Statement. */ private String constructFindLIKEPreparedStatement(Company criteriaCompany) { String vSQL = "SELECT ";
boolean bhasSet = false;
int setindex = 1; String vWhereVals = " ";
vSQL = vSQL + "companyid"; vSQL = vSQL + ",companyname";
if (criteriaCompany.CompanyID_isSet) { if (bhasSet) { vWhereVals = vWhereVals + " AND "; }
vWhereVals = vWhereVals + " companyid LIKE ? "; bhasSet = true; }
if (criteriaCompany.CompanyName_isSet) { if (bhasSet) { vWhereVals = vWhereVals + " AND "; }
vWhereVals = vWhereVals + " companyname LIKE ? "; bhasSet = true; }
vSQL = vSQL + " FROM tcompany ";
if (setindex > 0) { vSQL = vSQL + " WHERE "; }
vSQL = vSQL + vWhereVals;
return vSQL; }
private String constructInsertPreparedStatement(Company criteriaCompany) { String vSQL = "INSERT INTO tcompany("; boolean bhasSet = false;
int setindex = 1; String vInsertFields = " "; String vInsertValues = " ";
if (criteriaCompany.CompanyID_isSet) { if (bhasSet) { vInsertFields = vInsertFields + ",companyid"; vInsertValues = vInsertValues + ",?"; } else { vInsertFields = vInsertFields + "companyid"; vInsertValues = vInsertValues + "?"; bhasSet = true; } }
if (criteriaCompany.CompanyName_isSet) { if (bhasSet) { vInsertFields = vInsertFields + ",companyname"; vInsertValues = vInsertValues + ",?"; } else { vInsertFields = vInsertFields + "companyname"; vInsertValues = vInsertValues + "?"; bhasSet = true; } }
vSQL = vSQL + vInsertFields + ") VALUES (" + vInsertValues + ")";
return vSQL; }
private String constructUpdatePreparedStatement(Company criteriaCompany) { String vSQL = "UPDATE tcompany SET ";
boolean bhasSet = false;
int setindex = 1; String vSetVals = ""; String vWhereVals = "";
if (criteriaCompany.CompanyName_isSet) { if (bhasSet) { vSetVals = vSetVals + " , "; }
vSetVals = vSetVals + " companyname=?"; bhasSet = true; }
vSQL = vSQL + vSetVals; vSQL = vSQL + " WHERE companyid=?";
return vSQL; }
private String constructDeletePreparedStatement(Company criteriaCompany) { String vSQL = "DELETE FROM tcompany ";
boolean bhasSet = false;
int setindex = 1; String vWhereVals = " ";
if (criteriaCompany.CompanyID_isSet) { if (bhasSet) { vWhereVals = vWhereVals + " AND "; }
vWhereVals = vWhereVals + " companyid=?"; bhasSet = true; }
if (criteriaCompany.CompanyName_isSet) { if (bhasSet) { vWhereVals = vWhereVals + " AND "; }
vWhereVals = vWhereVals + " companyname=?"; bhasSet = true; }
if (setindex > 0) { vSQL = vSQL + " WHERE "; }
vSQL = vSQL + vWhereVals;
return vSQL; } }
|
|
|
|