0

I want to get some configuration values from SQL query:

enter image description here

Usually I use this Java code to get the data from table columns:

PreparedStatement ps = null;

try
{
    ps = conn.prepareStatement("SELECT US.USER_ID, S1.DESCRIPTION, S1.DATA_TYPE, CASE WHEN S1.CONSTRAINED = 'TRUE' THEN AV.ITEM_VALUE "
        + " ELSE US.UNCONSTRAINED_VALUE END \"VALUE\", AV.CAPTION FROM USER_SETTING US INNER JOIN SETTING S1 ON US.SETTING_ID = S1.ID "
        + " LEFT OUTER JOIN ALLOWED_SETTING_VALUE AV ON US.ALLOWED_SETTING_VALUE_ID = AV.ID WHERE US.USER_ID = 234");

    ResultSet rs = ps.executeQuery();
    while (rs.next())
    {
        obj = new SystemConfigurationObj(
            rs.getString("SNMP_SERVER"),
            rs.getInt("PORT"),
            rs.getString("SNMP_USERNAME"),
            rs.getString("SNMP_PASSWORD"),
            rs.getBoolean("IS_SSL")
        );
    }
}

How I can get the values from each row and cast it based on the value type?

I use this tables structure

    CREATE TABLE SETTING(
     ID INTEGER NOT NULL,
     DESCRIPTION TEXT,
     CONSTRAINED BOOLEAN,
     DATA_TYPE TEXT,
     MIN_VALUE TEXT,
     MAX_VALUE TEXT
    )
    ;

    ALTER TABLE SETTING ADD CONSTRAINT KEY34 PRIMARY KEY (ID)
    ;

    CREATE TABLE ALLOWED_SETTING_VALUE(
     ID INTEGER NOT NULL,
     SETTING_ID INTEGER,
     ITEM_VALUE TEXT,
     CAPTION TEXT
    )
    ;

    CREATE INDEX IX_RELATIONSHIP16 ON ALLOWED_SETTING_VALUE (SETTING_ID)
    ;


    ALTER TABLE ALLOWED_SETTING_VALUE ADD CONSTRAINT KEY35 PRIMARY KEY (ID)
    ;

    CREATE TABLE USER_SETTING(
     ID INTEGER NOT NULL,
     USER_ID INTEGER,
     SETTING_ID INTEGER,
     ALLOWED_SETTING_VALUE_ID INTEGER,
     UNCONSTRAINED_VALUE TEXT
    )
    ;

    CREATE INDEX IX_RELATIONSHIP15 ON USER_SETTING (SETTING_ID)
    ;

    CREATE INDEX IX_RELATIONSHIP17 ON USER_SETTING (ALLOWED_SETTING_VALUE_ID)
    ;

    ALTER TABLE USER_SETTING ADD CONSTRAINT KEY36 PRIMARY KEY (ID)
    ;

with
  allowed AS (
    INSERT INTO ALLOWED_SETTING_VALUE (id, setting_id, item_value, caption)
    VALUES 
    (123, 10, '#0000FF', 'Blue'), 
    (124, 10, '127.0.0.1', 'Yellow'), 
    (125, 10, '#FF00FF', 'Pink')
    RETURNING *),
  data(id, description, constrained, data_type, min_value, max_value, us_id, user_id, allowed_setting_value_id, unconstrained_value) as (
    VALUES
        (10, 'SMTP_SERVER', true, 'text', NULL, NULL, 5678, 234, (select id::Integer from allowed where caption = 'Yellow'), NULL),
        (11, 'SMTP_PORT', false, 'integer', '1', '65000', 7890, 234, NULL, '650'),
        (12, 'SNMP_USERNAME', false, 'text', '0', '9000', 8901, 234, NULL, 'username'),
        (13, 'SNMP_PASSWORD', false, 'text', '0', '9000', 8902, 234, NULL, 'password'),
        (14, 'IS_SSL', false, 'boolean', '0', '9000', 8903, 234, NULL, 'true')),
  settings as (
    INSERT INTO SETTING (id, description, constrained, data_type, min_value, max_value)
    SELECT id, description, constrained, data_type, min_value, max_value
    FROM data
    RETURNING *)
INSERT INTO USER_SETTING (id, user_id, setting_id, allowed_setting_value_id, unconstrained_value)
SELECT d.us_id, d.user_id, s.id, d.allowed_setting_value_id, d.unconstrained_value
FROM settings s
JOIN data d ON (d.id = s.id);
3
  • you are selecting SELECT US.USER_ID, S1.DESCRIPTION, S1.DATA_TYPE, and you need rs.getString("SNMP_SERVER"), rs.getInt("PORT"), rs.getString("SNMP_USERNAME"), rs.getString("SNMP_PASSWORD"), rs.getBoolean("IS_SSL")? Commented May 15, 2016 at 12:46
  • Are you sure? See the above picture. I use the above Java code to get rows of data. But in this case I have different case. Commented May 15, 2016 at 12:47
  • I don't think you are using the API correctly. The result set is a row of your results, so for example getInt(String columnLabel), but you seem to be expecting that do find the row, and not the column Commented May 15, 2016 at 12:55

1 Answer 1

1

you can get data_type first, then switch on it, like:

    ResultSet rs = ...;
    Map<String, Object> values = new HashMap<String, Object>();
    while (rs.next()) {
        Object o = null;
        String type = rs.getString("data_type");
        switch(type) {
        case "text":
            o = rs.getString("value");
            break;
        case "boolean":
            o = "true".equalsIgnoreCase(rs.getString("value"));
            break;
        case "int":
            o = rs.getInt("value");
            break;
        }
        values.put(rs.getString("description"), o);
    }

then you can create your object like

    new SystemConfigurationObj(
        (String)values.get("SNMP_SERVER"),
        (int)values.get("SMTP_PORT"),
        (String)values.get("SNMP_USERNAME"),
        (String)values.get("SNMP_PASSWORD"),
        (boolean)values.get("IS_SSL")
    );
Sign up to request clarification or add additional context in comments.

2 Comments

By the way is this slow operation? I will make this SQL query probably on every page load?
you can have a try, if you table only has rows like one in your picture, I think this absolutely do not affect your performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.