使用Commons Configuration

时间:2016-09-19 12:01:54

标签: java mysql postgresql apache-commons-config

我想在数据库表中存储一些属性。我正在使用 Apache Commons Configuration 2.1 。我找到了this,但我无法让它发挥作用......

我已经用Google搜索了,但我还没有找到任何文档或示例,而是我上面写的链接。

(编辑:代码现已修复)

这是数据库结构(postgresql):

-- Table: public.propcalendars

-- DROP TABLE public.propcalendars;

CREATE TABLE public.propcalendars
(
  name character varying NOT NULL,
  key character varying NOT NULL,
  value character varying,
  CONSTRAINT pk PRIMARY KEY (name, key)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE public.propcalendars
  OWNER TO postgres;

存储以下数据:

name            key     value
diasemana       SV2     0000010
fechafinal      SV2     31/08
fechainicial    SV2     01/07

这是我的代码:

public class PropertiesConfiguration {

    /*
     * Atributos
     */
    private static PropertiesConfiguration INSTANCE = null;

    private DatabaseConfiguration configFechaInicioAnual;
    private DatabaseConfiguration configFechaFinAnual;
    private DatabaseConfiguration configDiasSemana;

    /*
     * Métodos
     */
    private PropertiesConfiguration() throws ConfigurationException {
        configure();
    }

    private void configure() throws ConfigurationException {

        PGSimpleDataSource postgres = new PGSimpleDataSource();
        postgres.setServerName("ip");
        postgres.setDatabaseName("database");
        postgres.setPortNumber(5433);
        postgres.setUser("user");
        postgres.setPassword("***");
        postgres.setCurrentSchema("public");

        BasicConfigurationBuilder<DatabaseConfiguration> builder = new BasicConfigurationBuilder<DatabaseConfiguration>(DatabaseConfiguration.class);
        builder.configure(
            new Parameters()
            .database()
            .setDataSource(postgres)
            .setTable("propcalendars")
            .setKeyColumn("key")
            .setValueColumn("value")
            .setConfigurationNameColumn("name")
            .setConfigurationName("fechainicial")
        );

        configFechaInicioAnual = new DatabaseConfiguration();
        configFechaInicioAnual.setDataSource(postgres);
        configFechaInicioAnual.setKeyColumn("key");
        configFechaInicioAnual.setValueColumn("value");
        configFechaInicioAnual.setConfigurationName("fechainicial");

    }

    public static PropertiesConfiguration getInstance() throws ConfigurationException{
        if(INSTANCE == null){
            INSTANCE = new PropertiesConfiguration();
        }
        return INSTANCE;
    }

    public DatosCalendario getInfoCalendario(String pClaveCalendario, String pInfoCalendario){
        DatosCalendario dc = new DatosCalendario();
        dc.descripcion  = pInfoCalendario;
        dc.inicio       = configFechaInicioAnual.getString(pClaveCalendario);
        dc.fin          = "";//configFechaInicioAnual.getString(pClaveCalendario);
        dc.diasSemana   = 1;//configFechaInicioAnual.getInt(pClaveCalendario);

        return dc;
    }

    public class DatosCalendario{
        public String descripcion;
        public String inicio;
        public String fin;
        public Integer diasSemana;

        @Override
        public String toString(){
            return " DatosCalendario [ descripcion=" + descripcion + ", inicio=" + inicio + ", fin=" + fin + ", diasSemana=" + diasSemana + "]";
        }

    }

}

这是获得&#39; SV2&#39;与&#39; fechainicial&#39;:

  

java.sql.SQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#&quot; null&lt ;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在第1行       在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:686)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2041)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1827)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在com.mysql.cj.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1930)〜[mysql-connector-java-6.0.3.jar:6.0.3]       在org.apache.commons.configuration2.DatabaseConfiguration $ JdbcOperation.openResultSet(DatabaseConfiguration.java:847)〜[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.DatabaseConfiguration $ 1.performOperation(DatabaseConfiguration.java:333)〜[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.DatabaseConfiguration $ JdbcOperation.execute(DatabaseConfiguration.java:742)[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.DatabaseConfiguration.getPropertyInternal(DatabaseConfiguration.java:359)[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.AbstractConfiguration.getProperty(AbstractConfiguration.java:992)[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.AbstractConfiguration.getAndConvertProperty(AbstractConfiguration.java:1749)[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.AbstractConfiguration.convert(AbstractConfiguration.java:1785)[commons-configuration2-2.1.jar:2.1]       在org.apache.commons.configuration2.AbstractConfiguration.getString(AbstractConfiguration.java:1344)[commons-configuration2-2.1.jar:2.1]       在com.ingartek.gtfs.bizkaibus.configuration.PropertiesConfiguration.getInfoCalendario(PropertiesConfiguration.java:83)[classes /:?]       在com.ingartek.gtfs.bizkaibus.main.GenerarGtfs.main(GenerarGtfs.java:43)[classes /:?]   13:55:15.120 [主要] DEBUG com.ingartek.gtfs.bizkaibus.main.GenerarGtfs - DatosCalendario [descripcion = Sabados verano Julio Agosto,inicio = null,fin =,diasSemana = 1]

我做错了什么?有人可以提供额外的教程或文档吗?

正如@Thilo告诉我的那样,解决方案是使用一个表名而不包含一个&#34;连字符&#34; (&#34; - &#34)。不幸的是,它只在PostgreSQL和MySQL中工作我得到了例外:

  

java.sql.SQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在&#39; key =&#39; SV2&#39;附近使用正确的语法。 AND name =&#39; fechainicial&#39;&#39;在第1行

1 个答案:

答案 0 :(得分:1)

我在使用mysql时遇到了同样的问题

经过我们的调试后,我通过重命名&#39;键&#39;中的 KeyColumn 名称解决了这个问题。代码&#39;您可以使用其他一些列名,但不能使用&#39;键&#39;