Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <jsp:useBean id="opl" scope="page" class="libreria.dao.Model" />
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Biblioteca</title>
- </head>
- <body>
- <h1>Elenco libri</h1> <br>
- <jsp:getProperty name="opl" property="libri"/>
- </body>
- </html>
- package libreria.dao;
- import java.sql.*;
- import java.util.*;
- public class Model {
- private String URL = "jdbc:mysql://localhost:3306/Libreria";
- private String USER = "root";
- private String PWD = "";
- private Connection conn = null;
- public Model(){
- //empty constructor because i'm using JavaBean and i don't need it
- }
- public void startConnection(){
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(URL, USER, PWD);
- }catch(Exception e){}
- }
- public void endConnection(){
- try{
- conn.close();
- catch(Exception e){}
- }
- public String getLibri(){ /*method called from jsp (getProperty
- JavaBean)*/
- startConnection(); // **issue**
- Connection c = conn;
- String out = "";
- if(c != null){
- try {
- Statement st = c.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM libro");
- while (rs.next()) {
- out = out + "id_libro = " + rs.getInt("id_libro")
- + "; titolo = " + rs.getString("titolo")
- + "; autori = " + rs.getString("autori")
- + "; editore = " + rs.getString("editore")
- + "; disponibile = " +
- rs.getBoolean("disponibile")
- + "; in_prestito = " +
- rs.getBoolean("in_prestito")
- + "<br>";
- }
- rs.close();
- st.close();
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }else{
- return "conn null";
- }
- return out;
- }
- ...
- <%
- Model m = new Model();
- m.startConnection();
- %>
- <jsp:getProperty name="opl" property="libri"/>
- <%
- m.endConnection();
- %>
- ...
Add Comment
Please, Sign In to add comment