javascriptibm-mobilefirstmobilefirst-adaptersmobilefirst-server

MobileFirst 7.1 javascript adapter - how to read clob data?


function getUser(userId) {

    var obj = {};

    var resultSet = WL.Server.invokeSQLStatement({
        preparedStatement: getUserSQL,
        parameters: [userId]
    });

    obj = {
       name: result.resultSet[0]['NAME'],
       image: result.resultSet[0]['IMAGE'] // ???
    }

    return obj;
}

This returns clob.toString. I need to get the string (32K) from CLOB, how can I do this?


Solution

  • Hello, I've been able to solve the problem.

    I see that many people go through the same problems and I decided to share my code, I hope it helps.

    MFP 7.1 JavaScript SQL Adapter With Java

    function getUsuario(codigoUsuario) {
    
    var stream = { close: function () { } };
    var userInfo = {};
    
    var connection = getConnection();
    var getUsuarioSQL = 'SELECT * FROM DUAL'; // your sql
    
    if(connection !== null && !connection.erro) {
        try {
            var stmt = java.lang.Class.forName("java.sql.PreparedStatement").cast(connection.prepareStatement(getUsuarioSQL));
            stmt.setString(1, codigoUsuario);
    
            var rs = java.lang.Class.forName("java.sql.ResultSet").cast(stmt.executeQuery());
    
            while(rs.next()) {
                userInfo.nome = rs.getString("NOME_USUARIO");
                userInfo.codigoUsuario = rs.getString("COD_USUARIO");
                var clob = rs.getClob("AVATAR");
    
                var reader = java.lang.Class.forName("java.io.Reader").cast(clob.getCharacterStream());
                var bufferedReader = new java.io.BufferedReader(reader);
    
                var line = '';
                while((line = bufferedReader.readLine()) !== null) {
                    userInfo.avatar += line;
                }
            }
    
            return userInfo;
        } catch(erro) {
            return { erro: erro };
        } finally {
            stream.close();
            connection.close();
        }
    }
    
    return { isSuccessful: false, connection: connection };
    

    }

    function getConnection() {
        var connection = null;
    
        try {
            var context = new javax.naming.InitialContext();
            var dataSource = java.lang.Class.forName("javax.sql.DataSource").cast(context.lookup("YourJNDI"));
            connection = java.lang.Class.forName("java.sql.Connection").cast(dataSource.getConnection());
    
            return connection;
        } catch(erro) {
            WL.Logger.warn("Erro: " + erro);
            return {
                erro: erro
            };
        }
    
        return connection;
    }
    

    If someone has an idea to improve the code, share it. bye!