MobileFirst 7.1 javascript 适配器 - 如何读取 clob 数据?
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;
}
这个returnsclob.toString。我需要从 CLOB 获取字符串 (32K),我该怎么做?
您好,我已经解决了问题
我看到很多人遇到同样的问题,所以我决定分享我的代码,希望对您有所帮助。
MFP 7.1 Java脚本 SQL 适配器 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;
}
如果有人有改进代码的想法,请分享。
再见!
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;
}
这个returnsclob.toString。我需要从 CLOB 获取字符串 (32K),我该怎么做?
您好,我已经解决了问题
我看到很多人遇到同样的问题,所以我决定分享我的代码,希望对您有所帮助。
MFP 7.1 Java脚本 SQL 适配器 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;
}
如果有人有改进代码的想法,请分享。 再见!