📄 workflowdaoimpl.java~
字号:
/* */package com.sun.j2ee.workflow.user.dao;import java.sql.Connection;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.JNDINames;import com.sun.j2ee.workflow.user.dao.UserDAO;import com.sun.j2ee.workflow.util.DatabaseNames;import com.sun.j2ee.workflow.user.model.UserModel;import com.sun.j2ee.workflow.user.exceptions.UserDAOSysException;import com.sun.j2ee.workflow.user.exceptions.UserDAOAppException;import com.sun.j2ee.workflow.user.exceptions.UserDAODBUpdateException;import com.sun.j2ee.workflow.user.exceptions.UserDAOFinderException;import com.sun.j2ee.workflow.user.exceptions.UserDAODupKeyException;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 workflowDAOImpl implements UserDAO { private transient Connection dbConnection = null; private transient DataSource datasource = null; public workflowDAOImpl() throws UserDAOSysException { try { InitialContext ic = new InitialContext(); datasource = (DataSource) ic.lookup(JNDINames.WORKFLOW_DATASOURCE); } catch (NamingException ne) { throw new UserDAOSysException("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(UserModel details) throws UserDAOSysException, UserDAODupKeyException, UserDAODBUpdateException, UserDAOAppException { insertuser(details); } public UserModel load(String id) throws UserDAOSysException, UserDAOFinderException { return(selectuser(id)); } public void store(UserModel details) throws UserDAODBUpdateException, UserDAOAppException, UserDAOSysException { updateuser(details); } public void remove(String id) throws UserDAODBUpdateException, UserDAOSysException { deleteuser(id); } public String findByPrimaryKey(String userId) throws UserDAOFinderException, UserDAOSysException { if (workflowExists(userId)) return (userId); throw new UserDAOFinderException("primary key not found :"+workflowId); } private boolean workflowExists (String workflowId) throws UserDAOSysException { PreparedStatement stmt = null; ResultSet result = null; boolean returnValue = false; String queryStr ="SELECT userID FROM " + DatabaseNames.user_TABLE + " WHERE workflowid = " + "'" + workflowId.trim() + "'"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); result = stmt.executeQuery(); if ( !result.next() ) { returnValue = false; } else { workflowId = result.getString(1); returnValue = true; } } catch(SQLException se) { throw new UserDAOSysException( "SQLException while checking for an" + " existing workflow - id -> " + workflowId + " :\n" + se); } finally { closeResultSet(result); closeStatement(stmt); closeConnection(); } return returnValue; } private boolean isValidData(String workflowId, ContactInformation info) { if ( (workflowId == null) || ( info.getEMail() == null) || (info.getGivenName() == null) || (info.getFamilyName() == null) || (info.getAddress().getStreetName1() == null) || (info.getAddress().getCity() == null) || (info.getAddress().getState() == null) || (info.getAddress().getZipCode() == null) || (info.getAddress().getCountry() == null) || (info.getTelephone() == null) ) return (false); else return (true); } private void insertuser(UserModel details) throws UserDAOSysException, UserDAODupKeyException, UserDAODBUpdateException, UserDAOAppException { if (!isValidData(details.getworkflowId(), details.getContactInformation())) throw new UserDAOAppException("Illegal data values for insert"); if (workflowExists(details.getworkflowId())) throw new UserDAODupKeyException("user exists for "+ details.getworkflowId()); PreparedStatement stmt = null; ContactInformation info = details.getContactInformation(); String queryStr = "INSERT INTO " + DatabaseNames.user_TABLE + "(workflowid,email,firstname,lastname,status," + "addr1,addr2,city,state,zip,country," + "phone)" + "VALUES (" + "'" + details.getworkflowId().trim() + "'," + "'" + info.getEMail().trim() + "'," + "'" + info.getGivenName().trim() + "'," + "'" + info.getFamilyName().trim() + "'," + "'" + details.getStatus().trim() + "'," + "'" + info.getAddress().getStreetName1().trim() +"',"; if (info.getAddress().getStreetName2() != null) queryStr += "'"+info.getAddress().getStreetName2().trim() +"',"; else queryStr += "' ',"; queryStr += "'" + info.getAddress().getCity().trim() + "'," + "'" + info.getAddress().getState().trim() + "'," + "'" + info.getAddress().getZipCode().trim() + "'," + "'" + info.getAddress().getCountry().trim() + "'," + "'" + info.getTelephone().trim() + "' )"; Debug.println("queryString is: "+ queryStr); try { getDBConnection(); stmt = createPreparedStatement(dbConnection, queryStr); int resultCount = stmt.executeUpdate(); if ( resultCount != 1 ) { throw new UserDAODBUpdateException( "ERROR in user_TABLE INSERT !! resultCount = " + resultCount); } } catch(SQLException ae) { throw new UserDAOSysException( "SQLException while inserting new " + "user; id = " + details.getworkflowId() + " :\n" + ae); } finally { closeStatement(stmt); closeConnection(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -