Java代码中的SQL查询无法正常工作

时间:2016-01-21 09:50:03

标签: java sql oracle

我在JAVA中遇到SQL查询问题。

JAVA代码:

public boolean zeKontrolaExistujiciZalohyTest(String datum) {
        try {
            connected();
            boolean existujeZaloha = false;
            int pocet;
            ResultSet rs = statement.executeQuery("SELECT count(id) FROM "+table_ze+"\n" +
                            "WHERE TO_CHAR(TO_DATE(datum, 'dd.mm.yyyy'), 'mm.yyyy') = TO_CHAR(TO_DATE('"+datum+"', 'dd.mm.yyyy'), 'mm.yyyy')");            

            rs.next();
            pocet = rs.getInt(1);
            rs.close();
            closed();
            if (pocet >= 0) {
                existujeZaloha = true;
            } else {
                existujeZaloha = false;
            }
            return existujeZaloha;
        } catch (Exception e) {
            e.printStackTrace();
            Dialogs.create()
                .title("Exception Dialog")
                .showException(e);
            return true;
        }
    }

SQL Developer中的SQL查询:

SELECT count(id) FROM pbtest.u_zalohy_energie
WHERE TO_CHAR(TO_DATE(datum, 'dd.mm.yyyy'), 'mm.yyyy') = TO_CHAR(TO_DATE('15.09.2014', 'dd.mm.yyyy'), 'mm.yyyy');

当我运行JAVA代码时,结果变量是“pocet = 0”。但是,当我在任何SQL Developer中运行SQL查询时,结果列COUNT(id)为“1”。

当我更改SQL查询时,让我运行JAVA代码重新调整变量“pocet = 1”。

更改sql代码:

ResultSet rs = statement.executeQuery("SELECT count(id) FROM "+table_ze+"\n" +
            "WHERE datum = TO_DATE('"+datum+"', 'dd.mm.yyyy')");

有谁知道问题出在哪里?

有关信息:我使用的是Oracle数据库。

谢谢。

3 个答案:

答案 0 :(得分:1)

datum是字符串

SELECT count(id)
FROM pbtest.u_zalohy_energie
WHERE TO_DATE(datum, 'dd.mm.yyyy') = TO_DATE('15.09.2014', 'dd.mm.yyyy');

datum是日期

SELECT count(id)
FROM pbtest.u_zalohy_energie
WHERE TRUNC(datum) = TO_DATE('15.09.2014', 'dd.mm.yyyy');

如果datum是date,它也可能包含time组件。所以删除它。使用TRUNC()

TRUNC(datum) = TO_DATE('15.09.2014', 'dd.mm.yyyy');

Java代码:

ResultSet rs = statement.executeQuery("SELECT count(id) FROM "+table_ze+"\n" +
            "WHERE TRUNC(datum) = TO_DATE('"+datum+"', 'dd.mm.yyyy')");

作为旁注,请使用PreparedStatement and bind variables来避免SQL*Injection

答案 1 :(得分:0)

您的语句有语法错误

ResultSet rs = statement.executeQuery("SELECT count(id) FROM "+table_ze+"\n" +
"WHERE TO_CHAR(TO_DATE(datum, 'dd.mm.yyyy'), 'mm.yyyy') = TO_CHAR(TO_DATE('"+datum+"', 'dd.mm.yyyy'), 'mm.yyyy')");            

执行的查询将是

SELECT count(id) FROM pbtest.u_zalohy_energie\nWHERE TO_CHAR(TO_DATE(datum, 'dd.mm.yyyy'), 'mm.yyyy') = TO_CHAR(TO_DATE('15.09.2014'', 'dd.mm.yyyy'), 'mm.yyyy')")

你应该删除" \ n"因为这不会导致换行。

尝试

ResultSet rs = statement.executeQuery("SELECT count(id) FROM " + table_ze
+ " WHERE TO_CHAR(TO_DATE(datum, 'dd.mm.yyyy'), 'mm.yyyy') = TO_CHAR(TO_DATE('"+datum+"', 'dd.mm.yyyy'), 'mm.yyyy')");            

还要考虑Maheswaran Ravisankar的评论:" ... PreparedStatement和绑定变量以避免SQL *注入"

答案 2 :(得分:0)

感谢您的建议,我解决了以下问题:

public boolean zeKontrolaExistujiciZalohy(String datum, String typZalohy, String zalohaNaMesic) {
        connected();
        boolean existujeZaloha = false;
        int pocet = 0;
        ResultSet rs;
        PreparedStatement pstmt = null;
        try{
            statement = connection.createStatement();
            String SQL = "SELECT count(id) AS pocet FROM " + table_ze + " WHERE (EXTRACT(MONTH FROM datum)) = (EXTRACT(MONTH FROM to_date(?, 'dd.mm.yyyy'))) "
                    + "AND (EXTRACT(YEAR FROM datum)) = (EXTRACT(YEAR FROM to_date(?, 'dd.mm.yyyy')))"
                    + "AND typ_zalohy = ? "
                    + "AND zaloha_na_mesic = ? ";
            pstmt = connection.prepareStatement(SQL);
            pstmt.setString(1, datum);
            pstmt.setString(2, datum);
            pstmt.setString(3, typZalohy);
            pstmt.setString(4, zalohaNaMesic);
            rs = pstmt.executeQuery();
            while(rs.next()){
                pocet = rs.getInt("pocet");
            }
            rs.close();
            if (pocet > 0) {
                existujeZaloha = true;
            } else {
                existujeZaloha = false;
            }
            return existujeZaloha;            
        }
        catch(SQLException ex){
            Dialogs.create()
                .title("Exception Dialog")
                .showException(ex);
            return true;
        }
    }