Advertisement
Guest User

Untitled

a guest
Apr 27th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. /////////////// pocetak.jsp///////////////
  2.  
  3. <%--
  4. Document : pocetak
  5. Created on : Apr 21, 2016, 12:03:09 PM
  6. Author : Admin
  7. --%>
  8.  
  9. <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
  10. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  11.  
  12. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  13. <!DOCTYPE html>
  14. <html>
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  17. <title>JSP Page</title>
  18. </head>
  19. <body>
  20.  
  21. <form method="POST" action="ControllerSloj">
  22. <input type="number" name="idZupanije"/>
  23. <input type="submit" value="Dohvati" />
  24. </form>
  25. <br>
  26.  
  27.  
  28. <table border="1" width="50%">
  29. <tr>
  30. <th>Broj županije</th>
  31. <th>Županija</th>
  32. <th>Broj mjesta u županiji</th>
  33. </tr>
  34. <tr>
  35. <c:forEach var="rezultat" items="${rezultat}">
  36. <td><c:out value="${rezultat}"/></td>
  37. </c:forEach>
  38. </tr>
  39. </table>
  40. <br />
  41. </body>
  42. </html>
  43.  
  44. /////////////// controllerSloj servlet ///////////////
  45.  
  46.  
  47. @Override
  48. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50.  
  51. String idZupanijeS = null;
  52.  
  53. idZupanijeS = request.getParameter("idZupanije");
  54.  
  55. SpajanjeNaBazu spoj = new SpajanjeNaBazu();
  56.  
  57. if(spoj.checkID(idZupanijeS)) {
  58. List<String> rezultati = spoj.GetData(idZupanijeS);
  59.  
  60. request.setAttribute("rezultat", rezultati);
  61. }
  62. else {
  63. request.setAttribute("rezultat", "Unesite broj između 1 i 21!");
  64. }
  65.  
  66. RequestDispatcher view = request.getRequestDispatcher("pocetak.jsp");
  67. view.forward(request, response);
  68. }
  69.  
  70. /////////////// spajanjeNaBazu ///////////////
  71.  
  72. /*
  73. * To change this license header, choose License Headers in Project Properties.
  74. * To change this template file, choose Tools | Templates
  75. * and open the template in the editor.
  76. */
  77. package model;
  78.  
  79. import java.sql.Connection;
  80. import java.sql.DriverManager;
  81. import java.sql.ResultSet;
  82. import java.sql.SQLException;
  83. import java.sql.Statement;
  84. import java.util.ArrayList;
  85. import java.util.List;
  86.  
  87. /**
  88. *
  89. * @author Admin
  90. */
  91. public class SpajanjeNaBazu {
  92.  
  93. String url = "jdbc:mysql://localhost/wauj5";
  94.  
  95. public List<String> GetData(String idZupanije) {
  96. List<String> rezultati = new ArrayList<>();
  97.  
  98. try {
  99. Class.forName("org.mariadb.jdbc.Driver");
  100. } catch (ClassNotFoundException e) {
  101. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  102. }
  103.  
  104. try(Connection connection = DriverManager.getConnection(url, "root", "")) {
  105. Statement stmt = connection.createStatement();
  106. String query = "select zupanije.idzupanije, nazivzupanije, count(mjesta.idzupanije) as broj_mjesta from zupanije left join mjesta on (mjesta.idzupanije = zupanije.idzupanije) where zupanije.idzupanije = " + Integer.valueOf(idZupanije);
  107. ResultSet rs = stmt.executeQuery(query);
  108.  
  109. while(rs.next()) {
  110. rezultati.add(Integer.toString(rs.getInt("idzupanije")));
  111. rezultati.add(rs.getString("nazivzupanije"));
  112. rezultati.add(Integer.toString(rs.getInt("broj_mjesta")));
  113. }
  114.  
  115. rs.close();
  116. stmt.close();
  117. connection.close();
  118. } catch (SQLException e) {
  119. throw new IllegalStateException("Neuspjesno spajanje.", e);
  120. }
  121.  
  122. return rezultati;
  123. }
  124.  
  125. public boolean checkID(String idZupanijeS) {
  126.  
  127. Integer idZupanijeI;
  128.  
  129. try {
  130. idZupanijeI = Integer.valueOf(idZupanijeS);
  131. } catch (Exception e) {
  132. return false;
  133. }
  134.  
  135. return !(idZupanijeI > 21 || idZupanijeI < 1);
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement