是否可以使用 Java 为 Maximo 编写纯 sql 语句?目前我使用 myObj.setWhere("employee = 'brian'");
但我想做:
myObj.xxx("select * from employee where name = 'brian'");
有这样的功能吗?
虽然我强烈反对这样做,但您可以直接运行 SQL 语句,而无需自己与驱动程序管理器打交道。您可以从 Maximo 请求连接。
大多数时候(如以下示例中),Maximo 已经为您将运行的语句提供了功能,或者您将选择作为基本 ORM 的 Maximo 无法处理的字段子集。在这些情况下,您不会想要这样做。
Connection con = null;
try {
con = getMboServer().getDBConnection(getUserInfo().getConnectionKey());
PreparedStatement stmnt = con.prepareStatement("update asset set description = 'Hello World'");
stmnt.execute();
stmnt = con.prepareStatement("select count(*) from asset");
if (stmnt.execute()) {
ResultSet results = stmnt.getResultSet();
if (results.next()) {
int count = results.getInt(1);
}
}
} catch (SQLException e) {
logger.error("There was an 'SQLException' exception while getting the count of assets; The error is:\n", e);
} finally {
if (con != null) {
getMboServer().freeDBConnection(getUserInfo().getConnectionKey());
}
}
我认为 Maximo 可以使用 java.sql 包,它可以像这样使用:
import java.sql.*
String connectionString = "Your JDBC connection string here";
Connection conn = java.sql.DriverManager.getConnection(connectionString);
String sQuery = "SELECT SAMPLE_COLUMN FROM SAMPLE_TABLE";
Statement stmt= conn.createStatement();
ResultSet result = stmt.executeQuery(sQuery);
阅读此处如何解析 ResultSet 以获取您想要的信息:http: //docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html