I guess you are trying something like:
ResultSet rs = pstmt.executeQuery();
rs.first();
System.out.println(rs.getString("MY_COLUMN_NAME"));
You are getting this error because you are calling rs.next()
or rs.first()
as you said in the comments, even if there is no data in the resultset
.
So instead of try following code to avoid ResultSet not positioned properly, perhaps you need to call next.'
exception.
String myCustomMsg=""; //You can return this variable as custom message.
try
{
Connection con = GET_CONNECTION_HERE;
PreparedStatement pstmt = con.prepareStatement("QUERY HERE");
ResultSet rs = pstmt.executeQuery();
if(rs.next())// This will make sure that if there is no data, don't read the ResultSet
{
System.out.println(rs.getString("MY_COLUMN_NAME"));
myCustomMsg = "Ok";
}
else
myCustomMsg ="No Data found!";
}
catch(SQLException sqle)
{
sqle.printStackTrace();
myCustomMsg ="YOUR CUSTOM MESSAGE HERE....";
}