package DBLayer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import ModelLayer.City;
public class DBCity implements IFDBCity{
private Connection con;
public DBCity() {
con = DBConnection.getInstance().getDBcon();
}
private City buildCity(ResultSet results)
{
City city = new City();
try{
city.setZipCode(results.getInt("ZIPCode"));
city.setCity(results.getString("City"));
}
catch(Exception e)
{
System.out.println("error in building the City object");
System.out.println(e.getMessage());
}
return city;
}
//singleWhere
private City singleWhere(String wClause, boolean retrieveAssociation)
{
ResultSet results;
City cityObj = new City();
String query = buildQuery(wClause);
System.out.println(query);
try{ // read the city from the database
Statement stmt = con.createStatement();
stmt.setQueryTimeout(5);
results = stmt.executeQuery(query);
if( results.next() ){
cityObj = buildCity(results);
stmt.close();
if(retrieveAssociation)
{
}
}
}//end try
catch(Exception e){
System.out.println("Query exception: "+e);
}
return cityObj;
}
//buildQuery
private String buildQuery(String wClause)
{
String query="SELECT ZIPCode, City FROM ZIPCodes";
if (wClause.length()>0)
query=query+" WHERE "+ wClause;
return query;
}
//search city
public City findCity(int zipcity, boolean retriveAssociation)
{ String wClause = " ZIPCode = " + zipcity + "";
return singleWhere(wClause, retriveAssociation);
}
}