You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maximo自动化脚本跨库执行插入更新操作不生效问题咨询

问题根因及修复方案

核心错误点

  • 变量名大小写拼写错误:else分支中你定义的插入语句变量是sqlInsert(首字母I大写),但后续执行时用的是sqlinsert(首字母i小写),当走插入逻辑时实际执行的是空字符串,自然没有效果。
  • 执行方法调用错误:executeQuery()方法仅用于执行SELECT查询并返回结果集,INSERT、UPDATE这类DML写操作需要调用executeUpdate()方法才能生效。
  • 事务提交缺失:JDBC连接默认是否开启自动提交取决于驱动配置,稳妥的做法是执行完DML操作后显式调用connection.commit()提交事务,避免操作被回滚。
  • 未定义jdbc_driver变量:代码中加载驱动时用到的jdbc_driver变量没有赋值,会抛出类找不到异常,你需要先定义该变量,SQL Server对应的驱动类为com.microsoft.sqlserver.jdbc.SQLServerDriver。
  • 额外的格式错误:你定义的日期格式是yyyy-MM--dd,MM和dd之间多了一个横杠,会导致插入的STATUSDATE字段值格式错误。
  • SQL注入风险:直接拼接SQL字符串容易因为特殊字符报错,还存在注入风险,建议使用PreparedStatement预编译语句处理参数。

修复后核心代码示例

from psdi.security import UserInfo
from psdi.server import MXServer
from psdi.util import MXApplicationException
from psdi.util import MXException
from java.rmi import RemoteException
from java.lang import System
from java.text import Format, DateFormat, SimpleDateFormat
from java.lang import Class
from java.sql import DriverManager,SQLException

mx = MXServer.getMXServer()
ui = mx.getSystemUserInfo()

# 补充缺失的驱动类定义
jdbc_driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
url= "jdbc:sqlserver://MAXIMODEMO:1433; database=IntegrationTest; user=maxadmin; password=password; encrypt=false; trustServerCertificate=false; loginTimeout=30;"

# 加载驱动并注册
Class.forName(jdbc_driver).newInstance()
DriverManager.registerDriver(Class.forName(jdbc_driver).newInstance())

# 获取连接
connection = DriverManager.getConnection(url)
# 可选:显式开启自动提交,开启后不需要手动commit
# connection.setAutoCommit(True)

# 查询物料是否存在,使用预编译语句避免注入
check_sql = "Select itemnum from item where itemnum=?"
check_stmt = connection.prepareStatement(check_sql)
check_stmt.setString(1, mbo.getString("ITEMNUM"))
result = check_stmt.executeQuery()

sdf = SimpleDateFormat("yyyy-MM-dd") # 修正日期格式
exec_stmt = None

if(result.next()):
    # 走更新逻辑
    update_sql = "Update Item set description=?, orderunit=?, status=? where ItemNum=?"
    exec_stmt = connection.prepareStatement(update_sql)
    exec_stmt.setString(1, mbo.getString("DESCRIPTION"))
    exec_stmt.setString(2, mbo.getString("ORDERUNIT"))
    exec_stmt.setString(3, mbo.getString("STATUS"))
    exec_stmt.setString(4, mbo.getString("ITEMNUM"))
else:
    # 走插入逻辑
    insert_sql = "Insert into item(itemnum, description, orderunit, statusdate, status, groupname) values(?,?,?,?,?,?)"
    exec_stmt = connection.prepareStatement(insert_sql)
    exec_stmt.setString(1, mbo.getString("ITEMNUM"))
    exec_stmt.setString(2, mbo.getString("Description"))
    exec_stmt.setString(3, mbo.getString("ORDERUNIT"))
    exec_stmt.setString(4, sdf.format(mbo.getDate("STATUSDATE")))
    exec_stmt.setString(5, mbo.getString("STATUS"))
    exec_stmt.setString(6, mbo.getString("GROUPNAME"))

result.close()
check_stmt.close()

# 调用executeUpdate执行DML操作,返回受影响行数
affected_rows = exec_stmt.executeUpdate()
# 显式提交事务,如果没开自动提交必须加
connection.commit()

exec_stmt.close()
connection.close()

内容的提问来源于stack exchange,提问作者user8533094

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:24:01