Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private List<City> getCities() {
- final String driver = "com.mysql.jdbc.Driver";
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- List<City> cityList = null;
- final String dbPath = "jdbc:mysql://localhost:3306/world";
- final String sqlQuery = "SELECT Name, Population FROM city";
- Connection conn = null;
- Statement statement = null;
- ResultSet resultSet = null;
- try {
- conn = DriverManager.getConnection(dbPath, "root", "admin");
- statement = conn.createStatement();
- resultSet = statement.executeQuery(sqlQuery);
- String cityName = null;
- int cityPopulation = 0;
- cityList = new ArrayList<>();
- while (resultSet.next()) {
- cityName = resultSet.getString("Name");
- cityPopulation = resultSet.getInt("Population");
- City city = new City(cityName, cityPopulation);
- cityList.add(city);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- return cityList;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement