org.biblionum.authentification.modele.UtilisateurModele.java Source code

Java tutorial

Introduction

Here is the source code for org.biblionum.authentification.modele.UtilisateurModele.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.biblionum.authentification.modele;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.apache.commons.dbutils.BeanProcessor;
import org.biblionum.authentification.Utilisateur;

/**
 *
 * @author Stan
 */
public class UtilisateurModele {

    private Connection con;
    private PreparedStatement stm;
    private ResultSet rs;
    private String requete;

    /**
     * Java method that creates the generated table
     *
     * @param con (open java.sql.Connection)
     * @throws SQLException
     */

    public boolean createTableUtilisateur(DataSource ds) throws SQLException {

        Statement statement = con.createStatement();
        String sql = "CREATE TABLE utilisateur(id int AUTO_INCREMENT, " + "`nom` VARCHAR(255), "
                + "`password` VARCHAR(255), " + "`pseudo` VARCHAR(255), " + "`prenom` VARCHAR(255), "
                + "`utilisateur_type_id` INT,)";
        statement.execute(sql);
        statement.close();
        con.close();
        return true;
    }

    /**
     * Java method that inserts a row in the generated sql table and returns the
     * new generated id
     *
     * @param con (open java.sql.Connection)
     * @param nom
     * @param password
     * @param pseudo
     * @param prenom
     * @param utilisateur_type_id
     * @return id (database row id [id])
     * @throws SQLException
     */
    public int insertIntoUtilisateur(DataSource ds, String nom, String password, String pseudo, String prenom)
            throws SQLException {
        con = ds.getConnection();
        int generatedId = -1;
        String sql = "INSERT INTO utilisateur (nom, " + "password, pseudo, prenom)" + "VALUES (?, ?, ?, ?)";
        PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        statement.setString(1, nom);
        statement.setString(2, password);
        statement.setString(3, pseudo);
        statement.setString(4, prenom);

        statement.execute();
        ResultSet auto = statement.getGeneratedKeys();

        if (auto.next()) {
            generatedId = auto.getInt(1);
        } else {
            generatedId = -1;
        }

        statement.close();
        con.close();
        return generatedId;
    }

    /**
     * Java method that updates a row in the generated sql table
     *
     * @param con (open java.sql.Connection)
     * @param nom
     * @param password
     * @param pseudo
     * @param prenom
     * @param utilisateur_type_id
     * @return boolean (true on success)
     * @throws SQLException
     */
    public boolean updateUtilisateur(DataSource ds, int keyId, String nom, String password, String pseudo,
            String prenom) throws SQLException {
        con = ds.getConnection();
        String sql = "SELECT * FROM utilisateur WHERE id = ?";
        PreparedStatement statement = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        statement.setInt(1, keyId);
        ResultSet entry = statement.executeQuery();

        entry.last();
        int rows = entry.getRow();
        entry.beforeFirst();
        if (rows == 0) {
            entry.close();
            statement.close();
            con.close();
            return false;
        }
        entry.next();

        if (nom != null) {
            entry.updateString("nom", nom);
        }
        if (password != null) {
            entry.updateString("password", password);
        }
        if (pseudo != null) {
            entry.updateString("pseudo", pseudo);
        }
        if (prenom != null) {
            entry.updateString("prenom", prenom);
        }

        entry.updateRow();
        entry.close();
        statement.close();
        con.close();
        return true;
    }

    /**
     * Java method that deletes a row from the generated sql table
     *
     * @param con (open java.sql.Connection)
     * @param keyId (the primary key to the row)
     * @throws SQLException
     */
    public void deleteFromUtilisateur(DataSource ds, int keyId) throws SQLException {
        con = ds.getConnection();
        String sql = "DELETE FROM utilisateur WHERE id = ?";
        PreparedStatement statement = con.prepareStatement(sql);
        statement.setInt(1, keyId);
        statement.executeUpdate();
        statement.close();
        con.close();
    }

    /**
     * verifier si l'utilisateur existe
     *
     * @param pseudo
     */
    public boolean userExist(String pseudo, DataSource ds) {
        boolean exist = false;
        try {

            con = ds.getConnection();

            PreparedStatement sql = con.prepareStatement("SELECT id FROM utilisateur WHERE pseudo=?");
            sql.setString(1, pseudo);

            ResultSet rs = sql.executeQuery();
            while (rs.next()) {
                exist = true;
            }
            rs.close();
            con.close();

        } catch (SQLException ex) {
            Logger.getLogger(UtilisateurModele.class.getName()).log(Level.SEVERE, null, ex);
        }
        return exist;
    }

    /**
     * get utilisateur par le login
     */
    public Utilisateur getUtilisateurConnecter(DataSource ds, String pseudo, String pwd) {
        Utilisateur u = null;
        ArrayList<Utilisateur> list = new ArrayList<Utilisateur>();

        try {
            con = ds.getConnection();
            stm = con.prepareStatement("SELECT * FROM utilisateur WHERE pseudo=? AND password=?");
            stm.setString(1, pseudo);//type user prof 1
            stm.setString(2, pwd);
            rs = stm.executeQuery();

            BeanProcessor bp = new BeanProcessor();
            list = (ArrayList) bp.toBeanList(rs, Utilisateur.class);
            System.out.println("taille list utilisateur modele " + list.size());
            if (list.size() > 0) {
                u = list.get(0);
            }
            con.close();
            rs.close();

        } catch (SQLException ex) {
            Logger.getLogger(UtilisateurModele.class.getName()).log(Level.SEVERE, null, ex);
        }

        return u;
    }
}