Ayman
Ayman

Reputation: 81

SQL query returns no result from Java although it returns a result in Access

When I run this query:

select character from tbl_Unknown where format(fw,'.###')='48.143' and code='0001'

it returns a result in the Access query interface but when I try to run it from Java it doesn't return a result.

My table (tbl_Unknown):

char_id: autonumber   value:1
fw: short text        value:'48.1425'   Hint:after format it become '48.143'.
code: short text      value:'0001' 
character: short text value: 'x'

My java code:

public static String getLostedCharacter(String font,String fw, String code) {
      Connection conn = ConnectDB.getConnection();
      String character = null;
       try {
        Statement statement = conn.createStatement();
        String query = "select character from tbl_"+font+" where format(fw,'.###')='"+fw+"' and code='" + code + "'";
        ResultSet rs = statement.executeQuery(query);
         while (rs.next()) {
            character = rs.getString(1);
            return character;
        }
        statement.close();
        rs.close();
    } catch (SQLException ex) {
        return "";
    }
    return "";   
} 

Upvotes: 3

Views: 1191

Answers (2)

Gord Thompson
Gord Thompson

Reputation: 123429

Access SQL queries that are run from within the Access application itself can use a wide variety of VBA functions that may not be available (or may behave a bit differently) in Access SQL queries that are run from other applications.

As a workaround, I would suggest this:

String query = String.format(
        "select character from tbl_%s " +
        "where Trunc((Val(fw)*1000)+0.5)=? and code=?", 
        font);
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, (int)(1000 * Double.parseDouble(fw)));  // e.g. 48143
ps.setString(2, code);
ResultSet rs = ps.executeQuery();

Edit re: comments

As explained by jamadei, the Format() function as implemented in UCanAccess versions <= 2.0.6.2 produces slightly different results than the Access/VBA implementation for this particular case. Specifically Format(48.1425,".###) returns 48.143 in an Access query but it returns 48.142 in a UCanAccess query. This may be corrected in a future release of UCanAccess. This has been corrected in UCanAccess 2.0.6.3.

Upvotes: 2

jamadei
jamadei

Reputation: 1710

Both the mentioned issues(int function and rounding mode "half up" in the format function) have been fixed in the 2.0.6.3.

Upvotes: 1

Related Questions