agent-backend/contracts/MySQLExample/MySQLExample.yjs
2021-09-26 12:49:24 +08:00

27 lines
880 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@Permission("MySQL")
contract MySQLExample{
@Description("连接MySQL参数示例: {\"url\":"39.106.6.6:3306\",\"userName\":\"haiou\",\"pwd\":\"haiou\"}")
export function connectDBAndQuery(args){
args = JSON.parse(args);
var conn = MySQLUtil.getConnection(args.url,args.userName,args.pwd);
if (conn!=undefined && !conn.isClosed()){
print("successfully connected");
}
else return "connect failed";
var sql = "select * from haiou.catering limit 0,10";
var statement = conn.createStatement();
var resultSet = statement.executeQuery(sql);
ret = [];
var meta = resultSet.getMetaData();
for (;resultSet.next();){
var line = {
};
for (var j=1;j<=meta.getColumnCount();j++){
line[meta.getColumnName(j)] = resultSet.getString(j);
}
ret.push(line);
}
return JSON.stringify(ret);
}
}