📄 taskdaoimpl.java~
字号:
/* */package com.sun.j2ee.workflow.task.dao;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Iterator;import javax.sql.DataSource;import javax.naming.InitialContext;import javax.naming.Context;import javax.naming.NamingException;import com.sun.j2ee.workflow.util.JNDINames;import com.sun.j2ee.workflow.task.dao.TaskDAO;import com.sun.j2ee.workflow.util.DatabaseNames;import com.sun.j2ee.workflow.task.model.TaskModel;import com.sun.j2ee.workflow.task.exceptions.TaskDAOSysException;import com.sun.j2ee.workflow.task.exceptions.TaskDAOAppException;import com.sun.j2ee.workflow.task.exceptions.TaskDAODBUpdateException;import com.sun.j2ee.workflow.task.exceptions.TaskDAOFinderException;import com.sun.j2ee.workflow.task.exceptions.TaskDAODupKeyException;import com.sun.j2ee.workflow.util.Debug;/** * This class calls the simple Datasource from Tomcat to get connection * No connection pooling is used. */public class TaskDAOImpl implements TaskDAO { private transient Connection dbConnection = null; private transient DataSource datasource = null; public TaskDAOImpl() throws TaskDAOSysException { try { InitialContext ic = new InitialContext(); datasource = (DataSource) ic.lookup(JNDINames.WORKFLOW_DATASOURCE); } catch (NamingException ne) { throw new TaskDAOSysException("Naming Exception while looking " + " up DataSource Connection " + JNDINames.WORKFLOW_DATASOURCE + ": \n" + ne.getMessage()); } }/* public void query{ // load the PoolMan JDBC Driver try { Class.forName("com.codestudio.sql.PoolMan").newInstance(); } catch (Exception ex) { System.out.println("Could Not Find the PoolMan Driver. " + "Is poolman.jar in your CLASSPATH?"); System.exit(0); } // establish a Connection to the last database listed in the 'poolman.props' file Connection con = DriverManager.getConnection("jdbc:poolman"); try { Statement s = con.createStatement(); ResultSet res = s.executeQuery(sql); ResultSetMetaData meta = res.getMetaData(); int cols = meta.getColumnCount(); while (res.next()) { for (int i = 1; i <= cols; i++) { Object val = res.getObject(i); System.out.print("\t" + meta.getColumnLabel(i) + ": "); System.out.print(val == null ? " " : val.toString()); } System.out.print("\n"); } } catch (SQLException sqe) { } finally { // this close method merely returns the Connection to the pool // after implicitly closing related resources (Statements and ResultSets) con.close(); System.out.println("SAMPLE: Closed Con"); } } */ public void create(TaskModel taskinfo) throws TaskDAOSysException, TaskDAODupKeyException, TaskDAODBUpdateException, TaskDAOAppException { inserttask(taskinfo); } public TaskModel load(String id) throws TaskDAOSysException, TaskDAOFinderException { return(selecttask(id)); } public void store(TaskModel taskinfo) throws TaskDAODBUpdateException, TaskDAOAppException, TaskDAOSysException { updatetask(taskinfo); } public void remove(String id) throws TaskDAODBUpdateException, TaskDAOSysException { deletetask(id); } public String findByPrimaryKey(String taskId) throws TaskDAOFinderException, TaskDAOSysException { if (taskExists(taskId)) return (taskId); throw new TaskDAOFinderException("primary key not found :"+taskId); } private boolean taskExists (String taskId) throws TaskDAOSysException { PreparedStatement stmt = null; ResultSet result = null; boolean returnValue = false; String queryStr ="SELECT task_ID FROM " + DatabaseNames.TASK_TABLE + " WHERE task_ID = " + "'" + taskId.trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); if ( !result.next() ) { returnValue = false; } else { taskId = result.getString(1); returnValue = true; } } catch(SQLException se) { throw new TaskDAOSysException( "SQLException while checking for an" + " existing task - id -> " + taskId + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return returnValue; } private boolean isValidData(TaskModel taskinfo) { if ( (taskinfo.getTask_ID() == null) || (taskinfo.getTask_name() == null) || (taskinfo.getProject() == null) ) return (false); else return (true); } private void inserttask(TaskModel taskinfo) throws TaskDAOSysException, TaskDAODupKeyException, TaskDAODBUpdateException, TaskDAOAppException { if (!isValidData(taskinfo)) throw new TaskDAOAppException("Illegal data values for insert"); if (taskExists(taskinfo.getTask_ID())) throw new TaskDAODupKeyException("task exists for "+ taskinfo.getTask_ID()); PreparedStatement stmt = null; String queryStr = "INSERT INTO " + DatabaseNames.TASK_TABLE + "VALUES (" + "'" + taskinfo.getTask_ID().trim() + "'," + "'" + taskinfo.getTask_name().trim() + "'," + "'" + taskinfo.getPlan_start_dt().toString() + "'," + "'" + taskinfo.getPlan_end_dt().toString() + "'," + "'" + taskinfo.getAct_start_dt().toString() + "'," + "'" + taskinfo.getAct_end_dt().toString() + "'," + "'" + taskinfo.getStatus().trim() + "'," + "'" + taskinfo.getPredecessor().trim() + "'," + "'" + taskinfo.getProject().trim() + "'," + "'" + taskinfo.getTask_desc().trim() + "'," + "'" + taskinfo.getType().trim() + "'," + "'" + taskinfo.getComment().trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); int resultCount = stmt.executeUpdate(); if ( resultCount != 1 ) { throw new TaskDAODBUpdateException( "ERROR in TASK_TABLE INSERT !! resultCount = " + resultCount); } } catch(SQLException ae) { throw new TaskDAOSysException( "SQLException while inserting new " + "task; id = " + taskinfo.getTask_ID() + " :\n" + ae); } finally { closeStatement(stmt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -