3

Is it possible to write a pure sql statement for Maximo using Java? At the moment I use myObj.setWhere("employee = 'brian'");

But I'd like to do:

myObj.xxx("select * from employee where name = 'brian'");

is there such a function?

Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45

2 Answers2

3

While I would strongly discourage it, you can run direct SQL statements without needing to deal with the driver manager yourself. You can just request a connection from Maximo.

Most of the time (like in the below examples), Maximo already provides the functionality for the statement you would run, or you will be selecting a subset of fields that Maximo, as a basic ORM, can't handle. In those cases, you wouldn't want to do this.

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());
    }
}
Dex
  • 1,241
  • 6
  • 13
  • Yes I figured out it was a "bad" idea to run them directly. But thanks for response! – Baked Inhalf May 26 '14 at 09:08
  • From past experience, I plan on migrating database back ends about every 5 years. By sticking to "setwhere", you retain the database agnositicism that allows you to do this trivially. – pojo-guy Apr 02 '15 at 17:23
  • 1
    Actually, this is no less database agnostic than setWhere. I remember putting a lot of Oracle specific stuff in a setWhere while my very limited use of of this technique has been database agnostic selects with joins like this example. It's really all about how you use the tool. That being said, this has a higher likelihood of being misused. – Dex Apr 03 '15 at 23:05
0

I think Maximo will work with the java.sql package, which can be used like so:

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);

Read here how to parse through a ResultSet to get the information you want: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html

jlewkovich
  • 2,725
  • 2
  • 35
  • 49