agent-backend/contracts/MySQLExample/MySQLExample.yjs

27 lines
880 B
Plaintext
Raw Normal View History

2021-09-26 04:49:24 +00:00
@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);
}
}