Guest User

Untitled

a guest
Nov 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3.  
  4. <jsp:useBean id="opl" scope="page" class="libreria.dao.Model" />
  5.  
  6. <html>
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9. <title>Biblioteca</title>
  10. </head>
  11. <body>
  12.  
  13. <h1>Elenco libri</h1> <br>
  14.  
  15. <jsp:getProperty name="opl" property="libri"/>
  16.  
  17. </body>
  18. </html>
  19.  
  20. package libreria.dao;
  21.  
  22. import java.sql.*;
  23. import java.util.*;
  24.  
  25. public class Model {
  26.  
  27. private String URL = "jdbc:mysql://localhost:3306/Libreria";
  28. private String USER = "root";
  29. private String PWD = "";
  30. private Connection conn = null;
  31.  
  32. public Model(){
  33. //empty constructor because i'm using JavaBean and i don't need it
  34. }
  35.  
  36. public void startConnection(){
  37. try{
  38. Class.forName("com.mysql.jdbc.Driver").newInstance();
  39. conn = DriverManager.getConnection(URL, USER, PWD);
  40. }catch(Exception e){}
  41. }
  42.  
  43. public void endConnection(){
  44. try{
  45. conn.close();
  46. catch(Exception e){}
  47. }
  48.  
  49.  
  50. public String getLibri(){ /*method called from jsp (getProperty
  51. JavaBean)*/
  52. startConnection(); // **issue**
  53.  
  54. Connection c = conn;
  55.  
  56. String out = "";
  57.  
  58. if(c != null){
  59.  
  60. try {
  61. Statement st = c.createStatement();
  62. ResultSet rs = st.executeQuery("SELECT * FROM libro");
  63. while (rs.next()) {
  64. out = out + "id_libro = " + rs.getInt("id_libro")
  65. + "; titolo = " + rs.getString("titolo")
  66. + "; autori = " + rs.getString("autori")
  67. + "; editore = " + rs.getString("editore")
  68. + "; disponibile = " +
  69. rs.getBoolean("disponibile")
  70. + "; in_prestito = " +
  71. rs.getBoolean("in_prestito")
  72. + "<br>";
  73. }
  74. rs.close();
  75. st.close();
  76. } catch (SQLException e) {
  77. System.out.println(e.getMessage());
  78. }
  79.  
  80. }else{
  81. return "conn null";
  82. }
  83.  
  84. return out;
  85. }
  86.  
  87. ...
  88.  
  89. <%
  90. Model m = new Model();
  91. m.startConnection();
  92. %>
  93.  
  94. <jsp:getProperty name="opl" property="libri"/>
  95.  
  96. <%
  97. m.endConnection();
  98. %>
  99.  
  100. ...
Add Comment
Please, Sign In to add comment