简体   繁体   中英

SQL query in Java Code does not work properly

I have problem with SQL query in JAVA.

JAVA code:

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 query in SQL Developer:

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');

When I run JAVA code, so result a variable is "pocet = 0". But, when I run SQL query in any the SQL Developer, so result column COUNT(id) is "1".

When I do change the SQL query, let me run JAVA code retuns a variable "pocet = 1".

Change sql code:

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

Does anyone know where is the problem?

For information: I use an Oracle database.

Thank you.

datum is string

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

datum is date

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

If datum is date, it might contain time component too. So remove it. using TRUNC()

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

Java code:

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

As a side note, use PreparedStatement and bind variables to avoid SQL*Injection

Your statement has a syntax error

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')");            

the executed query would be

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')")

You should remove the "\\n" as this will not lead in a line break.

Try it as

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')");            

Take also into consideration the comment from Maheswaran Ravisankar about: "... PreparedStatement and bind variables to avoid SQL*Injection"

Thank you for your advice, I solved the problem as follows:

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM