我想从SQL查询中获取一些配置值:
通常我使用这个Java代码从表列中获取数据:
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")
);
}
}
如何从每一行获取值并根据值类型进行投射?
我使用这个表结构
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);
答案 0 :(得分:1)
你可以先获取data_type,然后打开它,如:
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);
}
然后你可以创建像
这样的对象 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")
);