学习使用Java Servlet连接到数据库进行编程

时间:2018-09-14 15:18:06

标签: java servlets

我正在学习用Java编程以及servlet和jsp页面的使用。

我在理解如何与数据库建立连接时遇到了一些麻烦。 特别是,我有一个名为Database.java的Java页面,在其中创建与数据库的连接,并在其中执行所有功能。 我创建了一个名为Prenotation.java的页面,在其中必须执行一些操作。我的问题是,我不想在该页面上保留数据库连接(从代码中可以看到),但是我想通过Database.java页面建立连接。 我已经尝试了几次,但我不知道该怎么做。 你能给我一些建议吗?谢谢。

Database.java

package db;
import java.math.BigDecimal;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import entity.*;

public class Database {

    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet rs = null;

    private String dbname = "Hotel";
    String nomeutente = "root";
    String password = "123456789";
     private static Database db = null;

    public static synchronized Database getDatabase() {
        if (db == null) {
            db = new Database();
        }
        return db;
    }
    private Database() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbname 
                    + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC",
                    nomeutente, password);
        } catch (Exception exc) {
            exc.printStackTrace();
        }
    }
    public Connection getConnection() {
        return connection;
    }
    public boolean checkUser(String email, String password) throws SQLException {
        boolean result = false;
        String query = "select password from users where email=?";

            statement = connection.prepareStatement(query);
            statement.setString(1, email);
            rs = statement.executeQuery();
            if (rs.next() && password.equals(rs.getString("password"))) {
                result = true;
            }

            rs.close();
            statement.close();

        return result;
    }
    public boolean existingMail(String email) throws SQLException {
        String query = "select * from users where email=?";
        boolean result = true;
        statement = connection.prepareStatement(query);
        statement.setString(1, email);
        rs = statement.executeQuery();

        if (rs.next()) {
            result = true;
        } else {
            result = false;
        }
        rs.close();
        statement.close();
        return result;
    }

    public boolean insertUtente(Utente u,String password) throws SQLException {
         String query = "INSERT INTO users (email,nome,cognome,luogodinascita,datadinascita,indirizzo,password) VALUES (?,?,?,?,?,?,?)";
         if(existingMail(u.getEmail())) {
             return false;
         } 
            statement = connection.prepareStatement(query);
            statement.setString(1, u.getEmail());
            statement.setString(2, u.getNome());
            statement.setString(3, u.getCognome());
            statement.setString(4, u.getLuogodinascita());
            statement.setString(5, u.getDatadinascita());
            statement.setString(6, u.getIndirizzo());
            statement.setString(7, password);
            statement.execute();
            statement.close();
            return true;

    }

    public Utente getUtente(String email) throws SQLException {
        String query= "select * from users where email=?";
        statement = connection.prepareStatement(query);
        statement.setString(1, email);
        rs=statement.executeQuery();
        if(!rs.next()) {
            return null;
        }
        Utente u=new Utente(email,rs.getString("nome"),rs.getString("cognome"),rs.getString("datadinascita"),rs.getString("luogodinascita"),rs.getString("indirizzo"));
        rs.close();
        statement.close();
        return u;
    }

    public boolean modificaPassword(String email, String password) throws SQLException {
        String query="UPDATE users SET password='"+password+"' WHERE email='"+email+"'";   
        Statement statement=connection.createStatement();
        statement.executeUpdate(query);
        statement.close();
        return true;
    }
    public boolean modificaProfilo(Utente u) throws SQLException {
        String query="UPDATE users SET nome = ?, cognome = ?, datadinascita = ?, luogodinascita = ?, indirizzo = ? WHERE email = ?";
        statement = connection.prepareStatement(query);
        statement.setString(1, u.getNome());
        statement.setString(2, u.getCognome());
        statement.setString(3, u.getDatadinascita());
        statement.setString(4, u.getLuogodinascita());
        statement.setString(5, u.getIndirizzo());
        statement.setString(6, u.getEmail());
        statement.executeUpdate();
        statement.close();
        return true;
    }}

这是我所说的页面

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import db.Database;
import entity.Prenotazione;
/**
 *
 * @author OOPs
 */
public class Prenotation extends HttpServlet {

    private static final String ResultSet = null;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session=request.getSession();
         String idPrenotazione = request.getParameter("idPrenotazione");
         String email = request.getParameter("email");
         int typeRoom = Integer.parseInt(request.getParameter("typeRoom"));;
         String arrivalDate = request.getParameter("arrivalDate");
         String departureDate = request.getParameter("departureDate");
         response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();

       try {

            Class.forName("com.mysql.cj.jdbc.Driver");
      //  out.println("driver loaded");
            Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Hotel?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root" ,"123456789");
            out.println("Connect");


            Statement  st =  con.createStatement();
           // Statement stmt = con.createStatement();
            out.println("connection successfull");
            int total = 0;
            PreparedStatement ps = con.prepareStatement( "SELECT COUNT(*) as total FROM reservation WHERE typeRoom = ? AND (? >= arrivaldate AND ? <= departuredate) OR (? >= arrivaldate AND ? <= departuredate)");

            int c = 0;
            ps.setInt(++c, typeRoom);
            ps.setString(++c, arrivalDate);
            ps.setString(++c, departureDate);
            ps.setString(++c, arrivalDate);
            ps.setString(++c, departureDate);

            ResultSet rs = ps.executeQuery();

           // ResultSet rs2  = stmt.executeQuery(check);
            out.println("<h1> Stringa check eseguito </h1>");

            if( total  > 0) { 
            //  response.sendRedirect("home.jsp");
                response.sendRedirect("PrenotazioneNegata.jsp");
            }
            else {
             st.executeUpdate("insert into reservation (email,typeRoom,arrivalDate,departureDate)values ('"+email+"','"+typeRoom+"','"+arrivalDate+"','"+departureDate+"')");
             response.sendRedirect("PrenotazioneAvvenuta.jsp");
            }
          out.println("<h1> Registrazione Eseguita </h1>");

        }catch(Exception e){
        out.println("Errore." +e);
        }
        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

0 个答案:

没有答案