package JAVA_PACKAGE; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import javax.sql.DataSource; import org.apache.log4j.Logger; public class Database&JAVA_CLASS&Manager implements JAVA_CLASS&ManagerIF { private DataSource dataSource; private static Logger logger = Logger.getLogger(Database&JAVA_CLASS&Manager.class.getName()); // Contains en Exception when an error has occured private Exception error; // Contains an error message private String message; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void create&JAVA_CLASS(JAVA_PROP_LIST) throws DAOException { if (get&JAVA_CLASS(JAVA_PKEY_ID) != null) throw new DAOException("Id " + JAVA_PKEY_ID + " is already used"); Connection conn = null; PreparedStatement pstmt = null; error = null; try { conn = dataSource.getConnection(); logger.info("Database&JAVA_CLASS&Manager.create&JAVA_CLASS, id=" + JAVA_PKEY_ID); // Prepare a statement to insert a record String sql = "INSERT INTO JAVA_OBJ_INSTANCE (JAVA_PROP_ID_LIST) VALUES(~java/qmark_list(JAVA_PROP_ID_LIST))"; pstmt = conn.prepareStatement(sql); @ @ list_process(JAVA_PROPERTIES,java/insert_set) @ pstmt.executeUpdate(); } catch (SQLException e) { error = e; message = "Create failed"; } closePrep(pstmt); closeConnection(conn); checkOK(); } public JAVA_CLASS get&JAVA_CLASS(JAVA_PKEY) throws DAOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet result = null; error = null; JAVA_CLASS JAVA_OBJ_INSTANCE = null; try { conn = dataSource.getConnection(); logger.info("Database&JAVA_CLASS&Manager.get&JAVA_CLASS, id=" + JAVA_PKEY_ID); // Prepare a statement to insert a record String sql = "SELECT * FROM JAVA_OBJ_INSTANCE WHERE JAVA_PKEY_ID=?"; pstmt = conn.prepareStatement(sql); @ @ list_process(JAVA_PKEYS,java/insert_set) @ result = pstmt.executeQuery(); if (result.next()) { @ @ list_process(JAVA_PROPERTIES,java/mk_new_obj) @ JAVA_OBJ_INSTANCE = new JAVA_CLASS(~java/property_id_list(JAVA_PROPERTIES,new)); } } catch (SQLException e) { error = e; message = "Read failed"; } closeResult(result); closePrep(pstmt); closeConnection(conn); checkOK(); return JAVA_OBJ_INSTANCE; } public void update&JAVA_CLASS(JAVA_PROP_LIST) throws DAOException { if (get&JAVA_CLASS(JAVA_PKEY_ID) == null) throw new DAOException("Id " + JAVA_PKEY_ID + " was not found"); Connection conn = null; PreparedStatement pstmt = null; error = null; try { conn = dataSource.getConnection(); logger.info("Database&JAVA_CLASS&Manager.update&JAVA_CLASS, id=" + JAVA_PKEY_ID); // Prepare a statement to update a record String sql = "UPDATE JAVA_OBJ_INSTANCE SET ~java/update_properties(sql_set) WHERE JAVA_PKEY_ID=?"; pstmt = conn.prepareStatement(sql); @ @ java/update_properties(java_set) @ pstmt.executeUpdate(); } catch (SQLException e) { error = e; message = "Update failed"; } closePrep(pstmt); closeConnection(conn); checkOK(); } public void delete&JAVA_CLASS(JAVA_PKEY) throws DAOException { if (get&JAVA_CLASS(JAVA_PKEY_ID) == null) throw new DAOException("Id " + JAVA_PKEY_ID + " was not found"); Connection conn = null; PreparedStatement pstmt = null; error = null; try { conn = dataSource.getConnection(); logger.info("Database&JAVA_CLASS&Manager.delete&JAVA_CLASS, id=" + JAVA_PKEY_ID); // Prepare a statement to delete a record String sql = "DELETE FROM JAVA_OBJ_INSTANCE WHERE JAVA_PKEY_ID=?"; pstmt = conn.prepareStatement(sql); @ @ java/delete_properties @ pstmt.executeUpdate(); } catch (SQLException e) { error = e; message = "Delete failed"; } closePrep(pstmt); closeConnection(conn); checkOK(); } public Collection getAll() throws DAOException { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; error = null; Collection c = new ArrayList(); try { conn = dataSource.getConnection(); logger.info("Database&JAVA_CLASS&Manager.getAll"); // Prepare a statement to delete a record String sql = "SELECT * FROM JAVA_OBJ_INSTANCE"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(sql); while (rs.next()) { @ java/copy_properties JAVA_CLASS JAVA_OBJ_INSTANCE = new JAVA_CLASS(JAVA_PROP_ID_LIST); c.add(JAVA_OBJ_INSTANCE); } } catch (SQLException e) { error = e; message = "Getall failed"; } closeResult(rs); closePrep(pstmt); closeConnection(conn); checkOK(); return c; } @ @ java/finder(dbmanager) @ private void checkOK() throws DAOException { if (error != null) { throw new DAOException(message, error); } } private void closeConnection(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { if (error == null) { error = e; message = "Close conn failed"; } } } } private void closePrep(PreparedStatement prep) { if (prep != null) { try { prep.close(); } catch (SQLException e) { if (error == null) { error = e; message = "Close prep failed"; } } } } private void closeResult(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { if (error == null) { error = e; message = "Close result failed"; } } } } }