package dao.impl;
import dao.ICountryDAO;
import dao.IDAO;
import dao.tro.Country;
import org.apache.log4j.Logger;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class HSQLDBCountryDAO extends IDAO implements ICountryDAO{
private Connection connection=null;
private static final Logger Logs = Logger.getLogger(HSQLDBAirportDAO.class);
public HSQLDBCountryDAO() {
connection = HSQLDBDAOFactory.getConnection();
}
public void insert(Country country) {
try {
String query = "INSERT INTO Countries VALUES (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setLong(1, country.getId());
statement.setString(2, country.getName());
statement.setString(3, country.getCapital());
statement.setString(4, country.getLanguage());
statement.executeUpdate(query);
} catch (SQLException e) {
Logs.info("Ошибка вставки " + e);
}
}
public void update(Country country) {
try {
String query = "UPDATE Countries SET Countries.Name = ?, Capital = ?, Counties.Language = ? WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setLong(4, country.getId());
statement.setString(1, country.getName());
statement.setString(2, country.getCapital());
statement.setString(3, country.getLanguage());
statement.executeUpdate(query);
} catch (SQLException e) {
Logs.info("Ошибка изменения " + e);
}
}
public void delete (long id) {
super.delete(id);
}
public Country showInformation(long id) {
Country country = new Country();
try {
String showStatement = "SELECT * FROM Countries WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(showStatement);
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
while(resultSet.next()) {
country.setId(id);
country.setCapital(resultSet.getString("Capital"));
country.setLanguage(resultSet.getString("Language"));
country.setName(resultSet.getString("Name"));
}
} catch(SQLException e) {
Logs.info("Ошибка выборки " + e);
}
return country;
}
@Override
public List<Country> allCountryList() {
List<Country> countryList = new ArrayList<Country>();
try {
String query = "SELECT Name FROM Countries";
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery();
int listIterator = 0;
while (resultSet.next()) {
countryList.add(new Country());
countryList.get(listIterator).setName(resultSet.getString("Name"));
listIterator++;
}
} catch (SQLException e) {
Logs.info("Ошибка загрузки списка стран");
}
return countryList;
}
@Override
public List<Country> countryList(int countryCount) {
List<Country> countryList = new ArrayList<>();
try {
String query = "SELECT * FROM Countries";
PreparedStatement statement = connection.prepareStatement(query);
ResultSet resultSet = statement.executeQuery();
int listIterator = 0;
while ((resultSet.next())&&(listIterator<countryCount)) {
countryList.add(new Country());
countryList.get(listIterator).setId(resultSet.getLong("Id"));
countryList.get(listIterator).setName(resultSet.getString("Name"));
countryList.get(listIterator).setCapital(resultSet.getString("Capital"));
countryList.get(listIterator).setLanguage(resultSet.getString("Language"));
listIterator++;
}
} catch (SQLException e) {
Logs.info("Ошибка загрузки списка стран (полный список)");
}
return countryList;
}
public String getTableName() {
return "Countries";
}
}