Java tutorial
/* * 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; /* Neccessary imports for the generated code */ 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 CategorieOuvrageModele { public static Connection con; public static PreparedStatement stm; public static ResultSet rs; public static String requete; /** * Java method that creates the generated table * * @param con (open java.sql.Connection) * @throws SQLException */ public static boolean createTableCategorieouvrage(DataSource ds) throws SQLException { con = ds.getConnection(); Statement statement = con.createStatement(); String sql = "CREATE TABLE categorieouvrage(id int AUTO_INCREMENT, " + "`designation_categorie` VARCHAR(255),)"; 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 designation_categorie * @return id (database row id [id]) * @throws SQLException */ public static int insertIntoCategorieouvrage(DataSource ds, String designation_categorie) throws SQLException { con = ds.getConnection(); int generatedId = -1; String sql = "INSERT INTO categorieouvrage (designation_categorie)" + "VALUES (?)"; PreparedStatement statement = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); statement.setString(1, designation_categorie); 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_categorie * @return boolean (true on success) * @throws SQLException */ public static boolean updateCategorieouvrage(DataSource ds, int keyId, String designation_categorie) throws SQLException { con = ds.getConnection(); String sql = "SELECT * FROM categorieouvrage 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_categorie != null) { entry.updateString("designation_categorie", designation_categorie); } 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 static void deleteFromCategorieouvrage(DataSource ds, int keyId) throws SQLException { con = ds.getConnection(); String sql = "DELETE FROM categorieouvrage WHERE id = ?"; PreparedStatement statement = con.prepareStatement(sql); statement.setInt(1, keyId); statement.executeUpdate(); statement.close(); con.close(); } /** * get all categorie */ public static ArrayList<CategorieOuvrage> getCategorieOuvrage(DataSource ds) { ArrayList<CategorieOuvrage> list = new ArrayList<CategorieOuvrage>(); try { con = ds.getConnection(); stm = con.prepareStatement("SELECT * FROM categorieouvrage"); rs = stm.executeQuery(); BeanProcessor bp = new BeanProcessor(); list = (ArrayList) bp.toBeanList(rs, CategorieOuvrage.class); con.close(); rs.close(); } catch (SQLException ex) { Logger.getLogger(UtilisateurModele.class.getName()).log(Level.SEVERE, null, ex); } return list; } }