private UserAndPassword executeStatementToUser ( PreparedStatement preparedStmt ) throws Exception { if ( preparedStmt . execute ( ) ) { // There's a ResultSet to be had ResultSet rs = preparedStmt . getResultSet ( ) ; ResultSetMetaData rsmd = rs . getMetaData ( ) ; int numColumns = rsmd . getColumnCount ( ) ; if ( numColumns != 5 ) { throw new Exception ( "Unexpected number of columns returned" ) ; } if ( false == rs . next ( ) ) { throw new Exception ( "Result set empty" ) ; } int userId = JdbcUtils . extractIntResult ( rs , rsmd , 1 ) ; String email = JdbcUtils . extractStringResult ( rs , rsmd , 2 ) ; String displayName = JdbcUtils . extractStringResult ( rs , rsmd , 3 ) ; String dbPassword = JdbcUtils . extractStringResult ( rs , rsmd , 4 ) ; int groupId = JdbcUtils . extractIntResult ( rs , rsmd , 5 ) ; if ( true == rs . next ( ) ) { throw new Exception ( "Result set had more than one result" ) ; } // Return user UserAndPassword user = new UserAndPassword ( ) ; user . setId ( userId ) ; user . setUser ( email ) ; user . setDisplayName ( displayName ) ; user . setPassword ( dbPassword ) ; if ( 0 == groupId ) { user . setAnonymous ( true ) ; } else if ( 1 == groupId ) { user . setAdmin ( true ) ; } Vector < Integer > groups = new Vector < Integer > ( ) ; groups . add ( new Integer ( groupId ) ) ; user . setGroups ( groups ) ; return user ; } else { // indicates an update count or no results - this must be no results throw new Exception ( "Query returned no results" ) ; } }
This method executes a prepared SQL statement against the user table and returns a User .