简体   繁体   中英

Oracle sql query works in sql developer but not in java

I have filters in a datatable and when user enters some value it should return a list of results matching that filter. I want it to be case-insensitive. I create the query string for a prepared statement via Java string concatenation, as in the below:

public static List<Logger> getAll(int from, int to, Map<String, Object> filters, String sortField,
        SortOrder sortOrder) {
    Connection con = null;
    PreparedStatement ps = null;
    List<Logger> lista = new ArrayList<>();
    String upit = "Select * from (select m.*,rownum r from (";
    String upitZaFilterISort = "select m.* from eps_stage.MDM_OSB_LOG m";

    try {
        con = DataConnect.getConnection();
        int upper = from + to;

        if (filters.size() > 0) {
            upitZaFilterISort = upitZaFilterISort.concat(" where 1=1");
            Set<String> keys = filters.keySet();

            // To get all key: value
            for (String key : keys) {
                if (key.equalsIgnoreCase("status") || key.equalsIgnoreCase("mbr")
                        || key.equalsIgnoreCase("pib") || key.equalsIgnoreCase("jmbg")
                        || key.equalsIgnoreCase("poruka_tip") || key.equalsIgnoreCase("aplikacija")
                        || key.equalsIgnoreCase("operacija")) {
                    upitZaFilterISort = upitZaFilterISort.concat(
                            " AND UPPER(" + key.toString() + ") LIKE '" + filters.get(key).toString().toUpperCase() + "%'");
                } 
            }
            }
        }

        String sort = "";

        ps = con.prepareStatement(upit + upitZaFilterISort + ") m ) where r>=? and r<=?");

        ps.setInt(1, from);
        ps.setInt(2, upper);

        System.out.println(upit+ upitZaFilterISort + sort+") m " + ") where r>=? and r<=?");

        ResultSet resultSet = ps.executeQuery();

In this line is a problem:

upitZaFilterISort = upitZaFilterISort.concat(
        " AND UPPER(" + key.toString() + ") LIKE '" + filters.get(key).toString().toUpperCase() + "%'");

When I use case-sensitive comparison it works:

upitZaFilterISort = upitZaFilterISort.concat(
        " AND " + key.toString() + " LIKE '" + filters.get(key).toString() + "%'");

After concatenation query:

Select * from (select m.*,rownum r from (select m.* from eps_stage.MDM_OSB_LOG m where 1=1 AND UPPER(poruka_tip) LIKE 'V%') m ) where r>=1 and r<=20

It returns the expected result when I run it in Oracle SQL Developer, but in my app it returns an empty result set.

Does Java put quotes somewhere I don't expect? I will provide more info if needed.

Re: https://docs.oracle.com/cd/B28359_01/appdev.111/b28843/tdddg_globalization.htm#CCHIJBCG , section "Changing NLS Parameter Values for All Sessions"

Please check your session settings for NLC_COMP, which may be set to LINGUISTIC. The link below can get you there in SQL Developer. If set to LINGUISTIC then your SQL Developer sessions are performing case insensitive searches, possibly explaining differences between the sessions.

Also, concur with Filippo's recomended practices.

Try this:

  1. Check if the user has all the required privileges to make the statements

  2. It may happen that the port has only one open connection. Therefore you can only use java or oracle sql developer. Try disconnecting from sql developer and running your java program. If it doesn't work tell me.

I hope it has been helpful

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