I started programming on js not so long ago. Tell me. Sampling countries, cities and regions from the MySQL database. Using scriptlets, I write them directly to the js code. (JSF do not want to use. Strange bit. For me personally, it is not clear.). It would seem that everything works out okay, but the question arises on performance. What is still better. Write directly to JS or use AJAX. Pros of Ajax in this case: - a more simplified sample of the database, less sweat about the code cons: - a permanent connection to the database to update the list of regions and cities

The advantages of writing directly from the database to js code: - when choosing a country, region, city, you do not need to contact the database every time. All cities and regions are recorded in variables. It only remains to get them using the function. Those. when sending a servlet to the user, the database writes to js the whole list and when choosing a country and regions, you do not have to bathe about the connection. Minuses: the darkness of the code, which at first glance is not very clear.

Here is the source of the servlet:

<script type="text/javascript"> var listCountryes = []; <%! MapLocation mapLocation;%> <% mapLocation = new MapLocation(); int numberListRegions = 0; int numberMapCountry = 0; for (Map.Entry<String, Map<String, List<String>>> entryCountry : mapLocation.getMapLocation().entrySet()) { //создается массив со странами %> let mapCountry_<%=numberMapCountry%> = new Map(); var listRegions_<%=numberListRegions%> = []; <% int numberMapRegion = 0; int numberListCity = 0; for (Map.Entry<String, List<String>> entryRegion : mapLocation.getMapLocation().get(entryCountry.getKey()).entrySet()) { // создаем переменную для листа с городами %> let mapRegions_<%=numberMapCountry%>_<%=numberMapRegion%> = new Map(); var listCityes_<%=numberListRegions%>_<%=numberListCity%> = []; <% // пробегаемся по листу с городами, добовляем каждый город в лист for (String city : mapLocation.getMapLocation() .get(entryCountry.getKey()) .get(entryRegion.getKey())) { %> listCityes_<%=numberListRegions%>_<%=numberListCity%>.push('<%=city%>'); <% } // конец пробежки по листу с городами, все добавили %> // добавляем полученный лист в регион мапу // [region, list_cityes] mapRegions_<%=numberMapCountry%>_<%=numberMapRegion%>.set('<%=entryRegion.getKey()%>', listCityes_<%=numberListRegions%>_<%=numberListCity%>); listRegions_<%=numberListRegions%>.push(mapRegions_<%=numberMapCountry%>_<%=numberMapRegion%>); <% numberListCity++; numberMapRegion++; } %> mapCountry_<%=numberMapCountry%>.set('<%=entryCountry.getKey()%>', listRegions_<%=numberListRegions%>); listCountryes.push(mapCountry_<%=numberMapCountry%>); <% numberListRegions++; numberMapCountry++; } %> alert("listCountryes - "+listCountryes); </script> 

The output in js is something like this (the rows of darkness are about 6 thousand):

 let mapCountry_0 = new Map(); var listRegions_0 = []; mapCountry_0.set('Белоруссия', listRegions_0); listCountryes.push(mapCountry_0); let mapCountry_1 = new Map(); var listRegions_1 = []; let mapRegions_1_0 = new Map(); var listCityes_1_0 = []; listCityes_1_0.push('Адыгейск'); listCityes_1_0.push('Майкоп (Адыгея)'); // добавляем полученный лист в регион мапу // [region, list_cityes] mapRegions_1_0.set('Адыгея', listCityes_1_0); listRegions_1.push(mapRegions_1_0); let mapRegions_1_1 = new Map(); var listCityes_1_1 = []; listCityes_1_1.push('Акташ'); listCityes_1_1.push('Акутиха'); listCityes_1_1.push('Алейск'); listCityes_1_1.push('Алтайский'); listCityes_1_1.push('Баево'); listCityes_1_1.push('Барнаул'); 

Here is the source connection to the database:

 public class MapLocation { private Map<String, Map<String, List<String>>> mapLocation; private Connection connection; public MapLocation() { connection = null; try { connection = DataBase.getConnection(); mapLocation = getMapCountry(connection); } catch (SQLException ex) { Logger.getLogger(MapLocation.class.getName()).log(Level.SEVERE, null, ex); } finally { try { if (connection!=null) connection.close(); } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } } } private Map<String, Map<String, List<String>>> getMapCountry(Connection connection) throws SQLException { Statement statement = null; ResultSet resultSet = null; Map<String, Map<String, List<String>>> mapCountry = null; try { StringBuilder sql = new StringBuilder(); sql.append("SELECT * FROM location_country"); sql.append(" ORDER BY name_country ASC"); statement = connection.createStatement(); resultSet = statement.executeQuery(sql.toString()); mapCountry = new LinkedHashMap<>(); while (resultSet.next()) { String nameCountry = resultSet.getString("name_country"); mapCountry.put(nameCountry, getMapRegion(nameCountry, connection)); } } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } finally { try { if (statement!=null) statement.close(); if (resultSet!=null) resultSet.close(); } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } } return mapCountry; } private Map<String, List<String>> getMapRegion(String nameCountry, Connection connection) { ResultSet resultSet = null; Statement statement = null; Map<String, List<String>> mapRegion = null; try { StringBuilder sql = new StringBuilder(); sql.append("SELECT * FROM location_region"); sql.append(" LEFT OUTER JOIN location_country ON location_region.index_country = location_country.id_country"); sql.append(" WHERE name_country = '"+nameCountry+"'"); sql.append(" ORDER BY name_region ASC"); statement = connection.createStatement(); resultSet = statement.executeQuery(sql.toString()); mapRegion = new LinkedHashMap<>(); while (resultSet.next()) { String nameRegion = resultSet.getString("name_region"); mapRegion.put(nameRegion, getListCity(nameRegion, connection)); } } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } finally { try { if (statement!=null) statement.close(); if (resultSet!=null) resultSet.close(); } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } } return mapRegion; } private List<String> getListCity(String nameRegion, Connection connection) { ResultSet resultSet = null; Statement statement = null; List<String> listCityInRegion = null; try { StringBuilder sql = new StringBuilder(); sql.append("SELECT * FROM location_city"); sql.append(" LEFT OUTER JOIN location_region ON location_city.index_region = location_region.id_region"); sql.append(" WHERE name_region = '"+nameRegion+"'"); sql.append(" ORDER BY name_city ASC"); statement = connection.createStatement(); resultSet = statement.executeQuery(sql.toString()); listCityInRegion = new LinkedList<>(); while (resultSet.next()) { listCityInRegion.add(resultSet.getString("name_city")); } } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } finally { try { if (statement!=null) statement.close(); if (resultSet!=null) resultSet.close(); } catch (SQLException ex) { Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); } } return listCityInRegion; } public Map<String, Map<String, List<String>>> getMapLocation() { return mapLocation; } } 

Gentlemen, options?

  • one
    I do not know how useful the advice is, but the darkness can be replaced by a simple declaration of an array of the form const c = ['Акташ','Акутиха,...'] - Dantessss
  • @Dantessss yes, this option is not bad. - Alexey Zemtsov

1 answer 1

Why does the user need to load all cities of all countries if he works only with the Moscow region?

The solution should definitely be on Ajax. And there are simply no other options.

What confuses you a constant connection to the database (especially if there is a pool of connections), I did not understand.