?? taskdaoimpl.java
字號(hào):
/* */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 java.util.Collection;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. * @author Jian (James) Cai */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; } public Collection findByName(String task_name) throws TaskDAOFinderException, TaskDAOSysException { PreparedStatement stmt = null; ResultSet result = null; TaskModel tmodel = null; boolean returnValue = false; ArrayList results = new ArrayList(); String queryStr ="SELECT * FROM " + DatabaseNames.TASK_TABLE + " WHERE task_name = " + "'" + task_name.trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); while(result.next()) { tmodel = new TaskModel(result.getString(1),result.getString(2), result.getDate(3),result.getDate(4), result.getDate(5), result.getDate(6),result.getString(7), result.getString(8), result.getString(9), result.getString(10), result.getString(11), result.getString(12)); results.add(tmodel); } } catch(SQLException se) { throw new TaskDAOSysException( "SQLException while selecting" + " existing task - name -> " + task_name + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return (results); } public Collection findByField(String fieldname, String fieldkey) throws TaskDAOFinderException, TaskDAOSysException { PreparedStatement stmt = null; ResultSet result = null; TaskModel tmodel = null; boolean returnValue = false; ArrayList results = new ArrayList(); String queryStr ="SELECT * FROM " + DatabaseNames.TASK_TABLE + " WHERE "+fieldname+" = " + "'" + fieldkey.trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); while(result.next()) { tmodel = new TaskModel(result.getString(1),result.getString(2), result.getDate(3),result.getDate(4), result.getDate(5), result.getDate(6),result.getString(7), result.getString(8), result.getString(9), result.getString(10), result.getString(11), result.getString(12)); results.add(tmodel); } } catch(SQLException se) { throw new TaskDAOSysException( "SQLException while selecting" + " existing task " + fieldname + " -> " + fieldkey + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return (results); } public Collection findAll() throws TaskDAOFinderException, TaskDAOSysException { PreparedStatement stmt = null; ResultSet result = null; TaskModel tmodel = null; boolean returnValue = false; ArrayList results = new ArrayList(); String queryStr ="SELECT * FROM " + DatabaseNames.TASK_TABLE; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); while(result.next()) { tmodel = new TaskModel(result.getString(1),result.getString(2), result.getDate(3),result.getDate(4), result.getDate(5), result.getDate(6),result.getString(7), result.getString(8), result.getString(9), result.getString(10), result.getString(11), result.getString(12)); results.add(tmodel); } } catch(SQLException se) { throw new TaskDAOSysException( "SQLException while selecting" + " existing all task -> " + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return (results); } private boolean isValidData(TaskModel taskinfo) { if ((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 Task"); /* if (taskExists(generatedID) throw new TaskDAODupKeyException("task exists for "+ taskinfo.getTask_ID()); */ PreparedStatement stmt = null; String generatedID = generateID();
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -