using ResultSet when row value from database is NULL - java

When querying the database directly, result is NULL, now Im using ResultSet to check if result is NULL, do something else, if result is not NULL, print the result: This my COde:
if(rs4 != null) {
while(rs4.next()) {
String ad =rs4.getString("number");
System.out.println(ad);
}
}
else{
System.out.println("ZERO ENTRIES");
}`
Database row value is NULL,since there is no row returned from my query so definitely i expect the else statement to run, but now the if statement is still being excecuted and prints null

What you see as NULL when querying the DB directly is the value of the column number, not the value of the row.
so, that's why you get a non-null row with a NULL value in number column in your ResultSet.

You can use something like:
if (!resultSet.isBeforeFirst() ) {
System.out.println("No data");
}
Or:
if (!resultSet.next() ) {
System.out.println("no data");
}

I finally found a way to go around it. Using rs.getInt() this will return 0 whenever row value is NULL and return new row value , whenever there is a row found.
This solved it :-)
while(rs.next())
{
// using rs.getInt() will return zero when row value from your query is NULL
int i = rs.getInt("number");
if(i<0) //check if zero
{
// When result is NULL i =0
System.out.println("your value");
}else{
//When there are rows found from database i > 0
String ad=rs.getString("number");
System.out.println(ad);
}
}`

Related

How to get multiple lines from mysql

I'm trying to fetch from mysql table multiple lines data, for example:
1 car
2 shop
3 dress
But for now it's only fetch the first line.
My code so far:
case "2":
ResultSet rsl2 = stmt.executeQuery("SELECT payments FROM payment;" );
while(rsl2.next()) {
String sqlRes = rsl2.getString("payments");
System.out.println(sqlRes);
if (sqlRes != null) {
System.out.println("OK");
return sqlRes;
}
}
You may either append all your data to the sqlRes variable
sqlRes += rsl2.getString("payments");
and change your if statement to
if (sqlRes == null)
return sqlRes;
or place your return statement after the while loop. Either way, you should append the data in the sqlRes variable if you finally want the returned value to hold all the values in your DB.

app crashes with cursor.getInt()

I have this method:
public String givenCheckmark(String name, String date)
{
SQLiteDatabase db = Cache.openDatabase();
Cursor cursor = db.rawQuery("select value from checkmarks where timestamp ='"+date+"' and habit ='"+name+"'", null);
String habitValue = "";
cursor.moveToFirst();
if (!(cursor == null)) {
habitValue = Integer.toString(cursor.getInt(0));
}
cursor.close();
return habitValue;
}
for some reason if i delete the
habitValue = Integer.toString(cursor.getInt(0));
the app does not crash. so the problem must be in the cursor.getInt(0). The query returns only one line. I am guessing the cursor is not in the first line but...why??
The most likely reason is the item at index "0" is not an Integer. Another reason is the Cursor itself may be empty. Finally, you probably have to check if there is actually a value returned.
To address the first part, you need to use Cursor#getColumnIndex() to get the index of the column you're trying to retrieve. So if it's an "id" you want, then you would do:
int id = cursor.getInt(cursor.getColumnIndex("id"));
To address the second part, you need to ensure that there were results returned. To do that, you just check Cursor#getCount(). So:
boolean isEmpty = cursor.getCount() == 0;
Alternatively, the return value for Cursor#moveToFirst() will actually return false if the cursor is empty meaning you can retrieve values by using something like this:
if (cursor != null && cursor.moveToFirst()) {
// The cursor is not null and contains elements. Continue retrieving values.
}
For the third part, you can use Cursor#isNull() to check if there is a value at the given index. So something like this:
boolean doesNotHaveValue = cursor.isNull(cursor.getColumnIndex("id"));
Other problems I see here is you're checking if the cursor is null after you move it to the first index meaning that you'll get a NullPointerException regardless of wether or not the cursor was null. You're also closing the cursor regardless of whether or not it is null.

java jdbc executeQuery() working but execute() not (multiple resultsets) [duplicate]

I'm calling a Sybase stored procedure that returns multiple resultsets through JDBC.
I need to get a specific result set that has a column named "Result"
This is my code :
CallableStatement cs = conn.prepareCall(sqlCall);
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
ResultSet rs=null;
int count = 1;
boolean flag = true;
while (count < 20000 && flag == true) {
cs.getMoreResults();
rs = cs.getResultSet();
if (rs != null) {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int columnsCount = resultSetMetaData.getColumnCount();
if (resultSetMetaData.getColumnName(1).equals("Result")) {
// action code resultset found
flag = false;
// loop on the resultset and add the elements returned to an array list
while (rs.next()) {
int x = 1;
while (x <= columnsCount) {
result.add(rs.getString(x));
x++;
}
}
result.add(0, cs.getString(1));
}
}
count++;
}
What happens here is that cs.getMoreResults returns a lot of null resultsets till it reaches the target one. I can't use cs.getMoreResults as loop condition because it returns false for null resultsets.
I put a fixed number to end the loop in condition the wanted result set wasn't returned to prevent it from going into infinite loop. It worked fine but I don't think this is right.
I think the null resultsets returned from the assignment in Sybase select #variable = value
Has anyone faced this before?
You are misinterpreting the return value of getMoreResults(). You are also ignoring the return value of execute(), this method returns a boolean indicating the type of the first result:
true: result is a ResultSet
false : result is an update count
If the result is true, then you use getResultSet() to retrieve the ResultSet, otherwise getUpdateCount() to retrieve the update count. If the update count is -1 it means there are no more results. Note that the update count will also be -1 when the current result is a ResultSet. It is also good to know that getResultSet() should return null if there are no more results or if the result is an update count (this last condition is why you get so many null values).
Now if you want to retrieve more results, you call getMoreResults() (or its brother accepting an int parameter). The return value of boolean has the same meaning as that of execute(), so false does not mean there are no more results!
There are only no more results if the getMoreResults() returns false and getUpdateCount() returns -1 (as also documented in the Javadoc)
Essentially this means that if you want to correctly process all results you need to do something like below:
boolean result = stmt.execute(...);
while(true) {
if (result) {
ResultSet rs = stmt.getResultSet();
// Do something with resultset ...
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
// Do something with update count ...
}
result = stmt.getMoreResults();
}
My guess is that you are getting a lot of update counts before you get the actual ResultSet.
I am not really familiar with Sybase, but its cousin SQL Server has the 'annoying' feature to return update counts from stored procedures if you don't explicitly put SET NOCOUNT ON; at the start of the stored procedure.
NOTE: Part of this answer is based on my answer to Execute “sp_msforeachdb” in a Java application

Invalid cursor exception in java when I am returning an object

I am unable to figure out some small issue that is there in my code. I am having a method that is returning multiple flags from a stored procedure.
Here is my code:
// QueryDataSet qds;
Object[] flags = null;
flags = (Call to a stored procedure that is returning multiple result sets)
if(flags != null)
{
for(int i = 0; i < flags.length; i++)
{
qds = ((QueryDataSet)flags[i]);
if(qds != null)
{
int result = qds.getRecord(0).getValue(1).asInt();
if(result>0) booleanflags[i]=true;
}
}
}
// getValue implementation is returning type of Record and getValue takes in the columnIndex. The stored procedure is returning multiple records and only one column and I am trying to check the count. If greater that 1 then return true else false.
// I am using the debugger and see that the first time in the loop the value is returned fine but when i=1 then it returns Invalid cursor position.

Null resultsets when calling Sybase stored procedure through JDBC

I'm calling a Sybase stored procedure that returns multiple resultsets through JDBC.
I need to get a specific result set that has a column named "Result"
This is my code :
CallableStatement cs = conn.prepareCall(sqlCall);
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
ResultSet rs=null;
int count = 1;
boolean flag = true;
while (count < 20000 && flag == true) {
cs.getMoreResults();
rs = cs.getResultSet();
if (rs != null) {
ResultSetMetaData resultSetMetaData = rs.getMetaData();
int columnsCount = resultSetMetaData.getColumnCount();
if (resultSetMetaData.getColumnName(1).equals("Result")) {
// action code resultset found
flag = false;
// loop on the resultset and add the elements returned to an array list
while (rs.next()) {
int x = 1;
while (x <= columnsCount) {
result.add(rs.getString(x));
x++;
}
}
result.add(0, cs.getString(1));
}
}
count++;
}
What happens here is that cs.getMoreResults returns a lot of null resultsets till it reaches the target one. I can't use cs.getMoreResults as loop condition because it returns false for null resultsets.
I put a fixed number to end the loop in condition the wanted result set wasn't returned to prevent it from going into infinite loop. It worked fine but I don't think this is right.
I think the null resultsets returned from the assignment in Sybase select #variable = value
Has anyone faced this before?
You are misinterpreting the return value of getMoreResults(). You are also ignoring the return value of execute(), this method returns a boolean indicating the type of the first result:
true: result is a ResultSet
false : result is an update count
If the result is true, then you use getResultSet() to retrieve the ResultSet, otherwise getUpdateCount() to retrieve the update count. If the update count is -1 it means there are no more results. Note that the update count will also be -1 when the current result is a ResultSet. It is also good to know that getResultSet() should return null if there are no more results or if the result is an update count (this last condition is why you get so many null values).
Now if you want to retrieve more results, you call getMoreResults() (or its brother accepting an int parameter). The return value of boolean has the same meaning as that of execute(), so false does not mean there are no more results!
There are only no more results if the getMoreResults() returns false and getUpdateCount() returns -1 (as also documented in the Javadoc)
Essentially this means that if you want to correctly process all results you need to do something like below:
boolean result = stmt.execute(...);
while(true) {
if (result) {
ResultSet rs = stmt.getResultSet();
// Do something with resultset ...
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
// Do something with update count ...
}
result = stmt.getMoreResults();
}
My guess is that you are getting a lot of update counts before you get the actual ResultSet.
I am not really familiar with Sybase, but its cousin SQL Server has the 'annoying' feature to return update counts from stored procedures if you don't explicitly put SET NOCOUNT ON; at the start of the stored procedure.
NOTE: Part of this answer is based on my answer to Execute “sp_msforeachdb” in a Java application

Categories