public User createUser(final User user, final String userPass) throws SQLException {

      Connection conn = null;
      PreparedStatement stmt = null;
      ResultSet rs = null;

      String nicename = user.slug;
      if(nicename.length() > 49) {
         nicename = nicename.substring(0, 49);
      }

      String username = user.username.length() < 60 ? user.username : user.username.substring(0, 60);

      Timer.Context ctx = metrics.createUserTimer.time();
      try {
         conn = connectionSupplier.getConnection();
         if(user.id > 0L) {
            stmt = conn.prepareStatement(createUserWithIdSQL);
            stmt.setLong(1, user.id);
            stmt.setString(2, username);
            stmt.setString(3, Strings.nullToEmpty(userPass));
            stmt.setString(4, nicename);
            stmt.setString(5, user.displayName());
            stmt.setString(6, Strings.nullToEmpty(user.email));
            stmt.setString(7, Strings.nullToEmpty(user.url));
            stmt.executeUpdate();
            return user;
         } else {
            stmt = conn.prepareStatement(createUserSQL, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1, username);
            stmt.setString(2, Strings.nullToEmpty(userPass));
            stmt.setString(3, nicename);
            stmt.setString(4, user.displayName());
            stmt.setString(5, Strings.nullToEmpty(user.email));
            stmt.setString(6, Strings.nullToEmpty(user.url));
            stmt.executeUpdate();
            rs = stmt.getGeneratedKeys();
            if(rs.next()) {
               return user.withId(rs.getLong(1));
            } else {
               throw new SQLException("Problem creating user (no generated id)");
            }
         }

      } finally {
         ctx.stop();
         closeQuietly(conn, stmt, rs);
      }
   }
public User createUser ( final User user , final String userPass ) throws SQLException { Connection conn = null ; PreparedStatement stmt = null ; ResultSet rs = null ; String nicename = user . slug ; if ( nicename . length ( ) > 49 ) { nicename = nicename . substring ( 0 , 49 ) ; } String username = user . username . length ( ) < 60 ? user . username : user . username . substring ( 0 , 60 ) ; Timer . Context ctx = metrics . createUserTimer . time ( ) ; try { conn = connectionSupplier . getConnection ( ) ; if ( user . id > 0L ) { stmt = conn . prepareStatement ( createUserWithIdSQL ) ; stmt . setLong ( 1 , user . id ) ; stmt . setString ( 2 , username ) ; stmt . setString ( 3 , Strings . nullToEmpty ( userPass ) ) ; stmt . setString ( 4 , nicename ) ; stmt . setString ( 5 , user . displayName ( ) ) ; stmt . setString ( 6 , Strings . nullToEmpty ( user . email ) ) ; stmt . setString ( 7 , Strings . nullToEmpty ( user . url ) ) ; stmt . executeUpdate ( ) ; return user ; } else { stmt = conn . prepareStatement ( createUserSQL , Statement . RETURN_GENERATED_KEYS ) ; stmt . setString ( 1 , username ) ; stmt . setString ( 2 , Strings . nullToEmpty ( userPass ) ) ; stmt . setString ( 3 , nicename ) ; stmt . setString ( 4 , user . displayName ( ) ) ; stmt . setString ( 5 , Strings . nullToEmpty ( user . email ) ) ; stmt . setString ( 6 , Strings . nullToEmpty ( user . url ) ) ; stmt . executeUpdate ( ) ; rs = stmt . getGeneratedKeys ( ) ; if ( rs . next ( ) ) { return user . withId ( rs . getLong ( 1 ) ) ; } else { throw new SQLException ( "Problem creating user (no generated id)" ) ; } } } finally { ctx . stop ( ) ; closeQuietly ( conn , stmt , rs ) ; } }
Creates a user.
<p>
If the {@code id} is &gt; 0, the user will be created with this id, otherwise it
will be auto-generated.
</p>
@param user The user.
@param userPass The {@code user_pass} string to use (probably the hash of a default username/password).
@return The newly created user.
@throws SQLException on database error.