mirror of
https://gitee.com/BDWare/agent-backend
synced 2025-01-10 09:54:11 +00:00
27 lines
880 B
Plaintext
27 lines
880 B
Plaintext
@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);
|
||
}
|
||
}
|