📄 abstractjdbcclinic.java
字号:
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
Specialty specialty = new Specialty();
specialty.setId(rs.getLong("id"));
specialty.setName(rs.getString("name"));
return specialty;
}
}
/**
* A particular <code>Vet</code>'s specialties Query Object.
*/
protected class VetSpecialtiesQuery extends MappingSqlQuery {
/**
* Creates a new instance of VetSpecialtiesQuery
* @param ds the DataSource to use for the query.
*/
protected VetSpecialtiesQuery(DataSource ds) {
super(ds, "SELECT specialty_id FROM vet_specialties WHERE vet_id=?");
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
return new Long(rs.getLong("specialty_id"));
}
}
/**
* Abstract base class for all <code>Owner</code> Query Objects.
*/
protected abstract class OwnersQuery extends MappingSqlQuery {
/**
* Creates a new instance of OwnersQuery
* @param ds the DataSource to use for the query.
* @param sql Value of the SQL to use for the query.
*/
protected OwnersQuery(DataSource ds, String sql) {
super(ds, sql);
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
Owner owner = new Owner();
owner.setId(rs.getLong("id"));
owner.setFirstName(rs.getString("first_name"));
owner.setLastName(rs.getString("last_name"));
owner.setAddress(rs.getString("address"));
owner.setCity(rs.getString("city"));
owner.setTelephone(rs.getString("telephone"));
return owner;
}
}
/**
* <code>Owner</code>s by last name Query Object.
*/
protected class OwnersByNameQuery extends OwnersQuery {
/**
* Creates a new instance of OwnersByNameQuery
* @param ds the DataSource to use for the query.
*/
protected OwnersByNameQuery(DataSource ds) {
super(ds, "SELECT id,first_name,last_name,address,city,telephone FROM owners WHERE last_name like ?");
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
}
/**
* <code>Owner</code> by id Query Object.
*/
protected class OwnerQuery extends OwnersQuery {
/**
* Creates a new instance of OwnerQuery
* @param ds the DataSource to use for the query.
*/
protected OwnerQuery(DataSource ds) {
super(ds, "SELECT id,first_name,last_name,address,city,telephone FROM owners WHERE id=?");
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
}
/**
* <code>Owner</code> Insert Object.
*/
protected class OwnerInsert extends SqlUpdate {
protected OwnerInsert(DataSource ds) {
super(ds, "INSERT INTO owners VALUES(?,?,?,?,?,?)");
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
protected void insert(Owner owner) {
Object[] objs =
new Object[]{
null,
owner.getFirstName(),
owner.getLastName(),
owner.getAddress(),
owner.getCity(),
owner.getTelephone()};
super.update(objs);
retrieveIdentity(owner);
}
}
/**
* <code>Owner</code> Update Object.
*/
protected class OwnerUpdate extends SqlUpdate {
/**
* Creates a new instance of OwnerUpdate
* @param ds the DataSource to use for the update.
*/
protected OwnerUpdate(DataSource ds) {
super(ds, "UPDATE owners SET first_name=?,last_name=?,address=?,city=?,telephone=? WHERE id=?");
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
/**
* Method to update an <code>Owner</code>'s data.
* @param owner to update.
* @return the number of rows affected by the update
*/
protected int update(Owner owner) {
return this.update(
new Object[]{
owner.getFirstName(),
owner.getLastName(),
owner.getAddress(),
owner.getCity(),
owner.getTelephone(),
new Long(owner.getId())});
}
}
/**
* Abstract base class for all <code>Pet</code> Query Objects.
*/
protected abstract class PetsQuery extends MappingSqlQuery {
/**
* Creates a new instance of PetsQuery
* @param ds the DataSource to use for the query.
* @param sql Value of the SQL to use for the query.
*/
protected PetsQuery(DataSource ds, String sql) {
super(ds, sql);
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
JdbcPet pet = new JdbcPet();
pet.setId(rs.getLong("id"));
pet.setName(rs.getString("name"));
pet.setBirthDate(rs.getDate("birth_date"));
pet.setTypeId(rs.getLong("type_id"));
pet.setOwnerId(rs.getLong("owner_id"));
return pet;
}
}
/**
* <code>Pet</code>s by <code>Owner</code> Query Object.
*/
protected class PetsByOwnerQuery extends PetsQuery {
/**
* Creates a new instance of PetsByOwnerQuery
* @param ds the DataSource to use for the query.
*/
protected PetsByOwnerQuery(DataSource ds) {
super(ds, "SELECT id,name,birth_date,type_id,owner_id FROM pets WHERE owner_id=?");
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
}
/**
* <code>Pet</code> by id Query Object.
*/
protected class PetQuery extends PetsQuery {
/**
* Creates a new instance of PetQuery
* @param ds the DataSource to use for the query.
*/
protected PetQuery(DataSource ds) {
super(ds, "SELECT id,name,birth_date,type_id,owner_id FROM pets WHERE id=?");
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
}
/**
* <code>Pet</code> Insert Object.
*/
protected class PetInsert extends SqlUpdate {
/**
* Creates a new instance of PetInsert
*/
protected PetInsert(DataSource ds) {
super(ds, "INSERT INTO pets VALUES(?,?,?,?,?)");
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.DATE));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
/**
* Method to insert a new <code>Pet</code>.
* @param pet to insert.
*/
protected void insert(Pet pet) {
Object[] objs =
new Object[]{
null,
pet.getName(),
new java.sql.Date(pet.getBirthDate().getTime()),
new Long(pet.getType().getId()),
new Long(pet.getOwner().getId()),
};
super.update(objs);
retrieveIdentity(pet);
}
}
/**
* <code>Pet</code> Update Object.
*/
protected class PetUpdate extends SqlUpdate {
/**
* Creates a new instance of PetUpdate
* @param ds the DataSource to use for the update.
*/
protected PetUpdate(DataSource ds) {
super(ds, "UPDATE pets SET name=?,birth_date=?,type_id=?,owner_id=? WHERE id=?");
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.DATE));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
/**
* Method to update an <code>Pet</code>'s data.
* @param pet to update.
* @return the number of rows affected by the update
*/
protected int update(Pet pet) {
return this.update(
new Object[]{
pet.getName(),
new java.sql.Date(pet.getBirthDate().getTime()),
new Long(pet.getType().getId()),
new Long(pet.getOwner().getId()),
new Long(pet.getId())
});
}
}
/**
* All <code>Pet</code> types Query Object.
*/
protected class PetTypesQuery extends MappingSqlQuery {
/**
* Creates a new instance of PetTypesQuery
* @param ds the DataSource to use for the query.
*/
protected PetTypesQuery(DataSource ds) {
super(ds, "SELECT id,name FROM types ORDER BY name");
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
PetType type = new PetType();
type.setId(rs.getLong("id"));
type.setName(rs.getString("name"));
return type;
}
}
/**
* <code>Visit</code>s by <code>Pet</code> Query Object.
*/
protected class VisitsQuery extends MappingSqlQuery {
/**
* Creates a new instance of VisitsQuery
* @param ds the DataSource to use for the update.
*/
protected VisitsQuery(DataSource ds) {
super(ds, "SELECT id,visit_date,description FROM visits WHERE pet_id=?");
declareParameter(new SqlParameter(Types.INTEGER));
compile();
}
protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
Visit visit = new Visit();
visit.setId(rs.getLong("id"));
visit.setDate(rs.getDate("visit_date"));
visit.setDescription(rs.getString("description"));
return visit;
}
}
/**
* <code>Visit</code> Insert Object.
*/
protected class VisitInsert extends SqlUpdate {
/**
* Creates a new instance of VisitInsert
*/
protected VisitInsert(DataSource ds) {
super(ds, "INSERT INTO visits VALUES(?,?,?,?)");
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.DATE));
declareParameter(new SqlParameter(Types.VARCHAR));
compile();
}
/**
* Method to insert a new <code>Visit</code>.
* @param visit to insert.
*/
protected void insert(Visit visit) {
Object[] objs =
new Object[]{
null,
new Long(visit.getPet().getId()),
new java.sql.Date(visit.getDate().getTime()),
visit.getDescription()
};
super.update(objs);
retrieveIdentity(visit);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -