开发者

ResultSet convert to int from query

开发者 https://www.devze.com 2023-03-22 19:51 出处:网络
So I am trying to get the result as count from a sql query as follows ResultSet rs = st.executeQuery(\"SELECT count(employeeID) FROM employee WHERE \" +

So I am trying to get the result as count from a sql query as follows

ResultSet rs = st.executeQuery("SELECT count(employeeID) FROM employee WHERE " +
                "employeeID='"+_empID+"' AND password = '"+_password + "'");

so i am also trying to convert that value to int and I tried the follwing

 for (; rs.next();) {
val =  (Integer) rs.getObject(1);
}

I have also try

val = Integer.parseInt(rs.getObject(1));

but nothing I get the following errors

java.lang.Long cannot be cast to java.lang.Integer

How can I do this.. so if that returns a 0,3 or 4 that it becomes an integer?

Thank you

EDITED TO: (STILL GETTING ERROR)

    long countLong = 0; 

        for (; rs.nex开发者_StackOverflow中文版t();) {
           countLong = rs.getLong(1);
        }


       if(countLong < 1)
       {
         isAuthentic = false;
       }
       else
       {
         isAuthentic = true;
       }


A good trick to use when you are not sure about the exact number type is to cast it to the parent class of all numeric type, Number:

val =  ((Number) rs.getObject(1)).intValue();

This will work for all numeric types, eg float, long, int etc.


Use ResultSet.getLong method:

long countLong = resultSet.getLong(1);
//if you really want and you are sure that it fits you can now cast
int count = (int)countLong; 


Try a getString() and then Long.parseLong().


Cast it to a string and then to an int or long or whatever you want:

Integer.parseInt(rs.getObject(1).toString());
0

精彩评论

暂无评论...
验证码 换一张
取 消