Search code examples
javamysqlapachetomcat6

My ResultSet is returning values but it is not storing values to string, why?


I am a beginner to programming, i am requesting to be kind and also requesting you to notify me, if there is any other mistakes in my code.

This code was working fine yesterday, i had updated some values in database today and now it is not working. on debugging i found that the resultset is returning values but now it fails stored in the String that i declared

public int startpgm(String xyz) {

    int i=0;

    String sp       = null;

    String Sb_SWLat = null;
    String Sb_NELat = null;
    String Sb_SWLon = null;
    String Sb_NELon = null;

    String qrySb = "select SW_lat, SW_lon, NE_lat, NE_lon from tbl_zones where zone='south'";

    try {
        Statement stmt = (Statement) conn.createStatement();

    ResultSet Sb=stmt.executeQuery(qrySb);
    if(Sb.next()){ 

        Sb_SWLat=Sb.getString("SW_lat");
        Sb_NELat=Sb.getString("NE_lat");
        Sb_SWLon=Sb.getString("SW_lon");            
        Sb_NELon=Sb.getString("NE_lon");

    }
    System.out.println("Sb_SWLat:"+Sb_SWLat);
    System.out.println("Sb_NELat:"+Sb_NELat);
    System.out.println("Sb_SWLon:"+Sb_SWLon);
    System.out.println("Sb_NELon:"+Sb_NELon);


    String qrysouth = "select priority from tbl_vcl where latitude >=? and latitude <=? and longitude >=? and longitude<=?";

    PreparedStatement pss = conn.prepareStatement(qrysouth);
    pss.setString(1, Sb_SWLat);
    pss.setString(2, Sb_NELat);
    pss.setString(3, Sb_SWLon);
    pss.setString(4, Sb_NELon);

    ResultSet rsSouth= pss.executeQuery();
    System.out.println("South ResultSet:"+rsSouth);

    if(rsSouth.next()){

    sp=rsSouth.getString("priority");
    System.out.println("South priority:"+sp);

    }   
    i=1;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
        return i ;
    }

And on executing program, This function is called from another page, my eclipse console:-

   Jan 18, 2016 6:35:58 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/53  config=null
Sb_SWLat: 9.993839
Sb_NELat: 9.994127
Sb_SWLon: 76.357213
Sb_NELon: 76.357329
Jan 18, 2016 6:35:58 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1018 ms
South ResultSet:com.mysql.jdbc.JDBC4ResultSet@c4728f

When i tried the query by manually entering the values in mysql console:-

mysql> select sum(priority) from tbl_vcl where latitude >=9.993839 and latitude <=9.994127 and longitude >=76.357213 and longitude<=76.357329;
+---------------+
| sum(priority) |
+---------------+
|             3 |
+---------------+

rsSouth != null and why i am getting sp=null in which sp=rsSouth.getString("sum(priority)") ? I am requesting you to correct me, if there is any other mistakes in my program or other best method to improve efficiency. Thanks in Advance.

DDL of my table:

mysql> desc tbl_vcl;
+-------------+-------------+------+-----+-------------------+-----------------------------+
| Field       | Type        | Null | Key | Default           | Extra                       |
+-------------+-------------+------+-----+-------------------+-----------------------------+
| pk_vcl_id   | int(11)     | NO   | PRI | NULL              | auto_increment              |
| vehicle_nm  | varchar(25) | YES  |     | NULL              |                             |
| reg_nu      | varchar(15) | YES  |     | NULL              |                             |
| officer_mob | varchar(20) | YES  |     | NULL              |                             |
| active      | int(11)     | YES  |     | NULL              |                             |
| updated_at  | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| latitude    | varchar(20) | YES  |     | NULL              |                             |
| longitude   | varchar(20) | YES  |     | NULL              |                             |
| priority    | varchar(2)  | YES  |     | NULL              |                             |
+-------------+-------------+------+-----+-------------------+-----------------------------+
9 rows in set (0.01 sec)

Solution

  • In your example query, you have

    ... where latitude >=9.993839 and ...

    This indicates that the fields in your table are of a numeric type. However, in your code, you use getString() to read the values and, more importantly, you use pse.setString(1, Eb_SWLat); to set the parameter values.

    You should be using getFloat() and setFloat() instead, because MySQL will have a hard time trying to compare float columns with string parameters.