org.biblionum.ouvrage.modele.OuvrageTypeModele.java Source code

Java tutorial

Introduction

Here is the source code for org.biblionum.ouvrage.modele.OuvrageTypeModele.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.ouvrage.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.modele.UtilisateurModele;

/**
 *
 * @author Stan
 */
public class OuvrageTypeModele {
    /* Neccessary imports for the generated code */

    public static 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
     */

    /**
     * Java method that inserts a row in the generated sql table and returns the
     * new generated id
     *
     * @param con (open java.sql.Connection)
     * @param designation_typeou
     * @return id (database row id [id])
     * @throws SQLException
     */
    public int insertIntoOuvragetype(DataSource ds, String designation_typeou) throws SQLException {
        con = ds.getConnection();
        int generatedId = -1;
        String sql = "INSERT INTO ouvragetype (designation_typeou)" + "VALUES (?)";
        PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        statement.setString(1, designation_typeou);
        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 designation_typeou
     * @return boolean (true on success)
     * @throws SQLException
     */
    public boolean updateOuvragetype(DataSource ds, int keyId, String designation_typeou) throws SQLException {
        con = ds.getConnection();
        String sql = "SELECT * FROM ouvragetype 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 (designation_typeou != null) {
            entry.updateString("designation_typeou", designation_typeou);
        }

        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 deleteFromOuvragetype(DataSource ds, int keyId) throws SQLException {
        con = ds.getConnection();
        String sql = "DELETE FROM ouvragetype WHERE id = ?";
        PreparedStatement statement = con.prepareStatement(sql);
        statement.setInt(1, keyId);
        statement.executeUpdate();
        statement.close();
        con.close();
    }

    public static ArrayList<OuvrageType> getTypeOuvrage(DataSource ds) {

        ArrayList<OuvrageType> list = new ArrayList<OuvrageType>();
        PreparedStatement stm;
        String requete;
        ResultSet rs;
        try {
            con = ds.getConnection();
            stm = con.prepareStatement("SELECT * FROM ouvragetype");

            rs = stm.executeQuery();

            BeanProcessor bp = new BeanProcessor();
            list = (ArrayList) bp.toBeanList(rs, OuvrageType.class);

            con.close();
            rs.close();

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

        return list;
    }
}