Created
November 25, 2011 08:56
-
-
Save hugithordarson/1393080 to your computer and use it in GitHub Desktop.
Smu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Create a callable statement from procedure string and parameters | |
* | |
* @param procedure String format for oracle procedure( i.e. {call VERE.Owner.OWNE_INSX ( ?, ... ) } ) | |
* @param parameters NSArray containing the parameters in the same order the procedure expects them | |
* @throws SQLException | |
*/ | |
public static CallableStatement createCallableStatementWithParametersFromArray( String procedure, NSArray parameters ) throws SQLException { | |
// We use the connection information in the Ekja EOModel. | |
EOModel theModel = EOModelGroup.defaultGroup().modelNamed( "Ekja" ); | |
NSDictionary connectionDictionary = theModel.connectionDictionary(); | |
String url = (String)connectionDictionary.objectForKey( USPrincipalClass.CONN_DICT_KEY_URL ); | |
String username = (String)connectionDictionary.objectForKey( USPrincipalClass.CONN_DICT_KEY_USERNAME ); | |
String password = (String)connectionDictionary.objectForKey( USPrincipalClass.CONN_DICT_KEY_PASSWORD ); | |
Connection conn = DriverManager.getConnection( url, username, password ); | |
conn.setAutoCommit( true ); | |
CallableStatement c = conn.prepareCall( procedure ); | |
int i = 0; | |
// Set parameters to the callable statement according to the parameter object types | |
for( Object o : parameters ) { | |
if( o instanceof NullObject ) | |
c.setObject( ++i, null ); | |
else if( o instanceof OutParameter ) { | |
OutParameter currentObject = (OutParameter)o; | |
if( currentObject.object instanceof Integer ) { | |
c.setInt( ++i, (Integer)currentObject.object ); | |
c.registerOutParameter( i, java.sql.Types.INTEGER ); | |
} | |
else if( currentObject.object instanceof String ) { | |
c.setString( ++i, (String)currentObject.object ); | |
c.registerOutParameter( i, java.sql.Types.VARCHAR ); | |
} | |
} | |
else if( o instanceof String ) | |
c.setString( ++i, (String)o ); | |
else if( o instanceof Date ) | |
c.setDate( ++i, (Date)o ); | |
else if( o instanceof NSTimestamp ) { | |
c.setTimestamp( ++i, (NSTimestamp)o ); | |
} | |
else if( o instanceof Long ) | |
c.setLong( ++i, (Long)o ); | |
else if( o instanceof Integer ) | |
c.setInt( ++i, (Integer)o ); | |
else { | |
throw new SQLException( "Unable to create callable statement for paramter " + o ); | |
} | |
} | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment