使用LIKE Postgres JDBC

时间:2016-03-27 15:46:46

标签: java postgresql jdbc

我有一个像这样的简单测试:

public Connection connect(String url, String user, String port, String password, String database) {
      debug("Try to connect at " + user+"@"+url+":"+port+"/"+database); 
      Connection connection = null;
      try {
         Class.forName("org.postgresql.Driver");
         connection= DriverManager.getConnection("jdbc:postgresql://"+url+":"+port+"/"+database, user, password);
      } catch (Exception e) {
         error(e.getClass().getName()+": "+e.getMessage());
      }
      debug("Database Connected."); 
      return connection;
}


public boolean executeSql( Connection connection, String sql ) {
    Statement stmt = null;
    try {
        stmt = connection.createStatement();
        stmt.executeUpdate(sql);
        stmt.close();
        connection.close();
        return true;
    } catch ( Exception e ) {
        error( e.getClass().getName()+": "+ e.getMessage() );
        return false;
    }
}

并尝试使用此内容运行查询:

( awk_out.optifunc like '%omega%' or awk_out.optifunc like '%chi%' or awk_out.optifunc like '%SIZE%' or awk_out.optifunc like '%d!_%' ESCAPE '!' )

但是我收到了这个错误:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "%"

如何在不使用预准备语句的情况下运行带有“%”字符的查询? 注意:我不能使用任何东西,只是我放在这里(没有休眠,没有准备好的陈述......)

编辑:完成查询(现在我可以看到错误!!!) 在提交到SQL引擎之前,未替换%ID_PIP%标记。

insert into select_geni_out (id_instance, id_experiment, id_activity, 
adjacency, laplacian, slaplacian, adjacencyB, laplacianB, slaplacianB,
 optifunc, caixa1, gorder, mindegree, maxdegree, trianglefree, allowdiscgraphs,
 biptonly, g6file, g6fileid, g6splitedfile, maxresults, chromatic, chromaticb,
 click, clickb, largestdegree, numedges) select %ID_PIP%, 260, 2857, 
awk_out.adjacency,awk_out.laplacian, awk_out.slaplacian, awk_out.adjacencyb, 
awk_out.laplacianb, awk_out.slaplacianb, awk_out.optifunc, awk_out.caixa1, 
awk_out.gorder, awk_out.mindegree, awk_out.maxdegree, awk_out.trianglefree, 
awk_out.allowdiscgraphs, awk_out.biptonly, awk_out.g6file, awk_out.g6fileid, 
awk_out.g6splitedfile, awk_out.maxresults, awk_out.chromatic, 
awk_out.chromaticb, awk_out.click, awk_out.clickb, awk_out.largestdegree 
,awk_out.numedges from awk_out where awk_out.id_experiment =  260 and ( 
awk_out.optifunc like '%omega%' or awk_out.optifunc like '%chi%' or 
awk_out.optifunc like '%SIZE%' or awk_out.optifunc like '%d!_%' ESCAPE '!' )

0 个答案:

没有答案