I understand that using statement.getResultSet() returns the result set as well as statement.executeQuery() but what happens if both are used like executeQuery() is called first and then getResultSet() is called after does it work or will it throw an exception that the result set has already been returned?
Please look into this
executeQuery: Returns one ResultSet object.
ResultSet rs = stmt.executeQuery(query);
Related
I have a snippet like this:
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM table_A");
statement.closeOnCompletion();
System.out.println(statement.isCloseOnCompletion());
rs.close();
System.out.println(statement.isClosed());
After closeOnCompletion is invoked, I expect the statement to be closed, as its result set is closed. However, it just shows "false" when checking statement.isClosed().
Did I misunderstand the jdk doc? as it says: closeOnCompletion() Specifies that this Statement will be closed when all its dependent result sets are closed.
===========================
Update:
It turns out to be my carelessness. The last statement actually returns a result of expected "true". So it's no longer an issue
Since Statement is depend on result I would suggest this
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM table_A");
rs.close();
statement.close();
System.out.println(statement.isClosed());
This is applicable if your ultimate want is to close statement, if your question is why statement is not closing even when called onexit, then better to refer javadocs.
I am trying to do a SELECT using PreparedStatement, but not getting any results back, despite the data being present. If I use Statement instead, I get the desired results, so not sure why I don't get anything with the PreparedStatement.
Any ideas?
Here is my code:
String species = "Snail";
PreparedStatement preparedStatement = con.prepareStatement("Select * from lifeforms where species=?",PreparedStatement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, species);
preparedStatement.execute();
ResultSet resultSet = preparedStatement.getGeneratedKeys();
Also - I have tried just
resultSet = preparedStatement.execute();
But this won't compile, saying I must change resultSet to expect a boolean value.
Any insights into this would be greatly appreciated.
Sorry if this is something obvious, but I have read the docs until I am almost unconscious, and I'm only just learning java.
The execute() functon returns a boolean value so u won't be able to put this in an object of resultset, if you want to execute a select statement you have to use executeQuery().
Ah so I have finally found the answer to this question - in case anyone else is stuck on the same thing..
The execute() method has no return value, although the compiler was suggesting it expected a boolean return value. Maybe it just returns true or false for successful or not?
The executeUpdate() method returns an int value of the number of records updated/inserted or deleted.
To get the result set of a query statement (e.g. Select * from someTable), use the
ResultSet rs = preparedStatement.executeQuery() method.
This will return the actual query results into a ResultSet variable.
I have a little problem with creating a method which takes String as parameter and return ResultSet after execute the query.Here is an example actually what I'm trying to do :
public ResultSet executeSQLQuery(String query){
ResultSet rs = sqliteDb.rawQuery(query,null);;
return rs;
}
Something like this, but it says that I have to convert rs to cursor type, but I need to return RS. Any ideas how to get the things to work?
possible duplicate
The result set isn't available, at least for now, in sqlite. It all depends on exactly what information you want from the ResultSet or ResultSetMetaData, etc, but there are other means of obtaining almost the same information.
check this link
to get resultset from pre-complie statement in android
Suppose we have this query:
ResultSet result = ("select * from table_name");
Now we can get information from result in this form
while(result.next())
{
.
.
.
}
but in this form we can go forward. Now I want to know if it possible that result get back?
For example we get information name='sam', fname='nic'. And for next I want go back and get this information again.
Is it possible?
Yes, JDBC supports that. You need to a scrollable resultset.
Something like this:
Statement st = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = st.executeQuery("select * from table_name");
Then you just use ResultSet object rs to move within your results:
first
absolute
last
next
previous
Are methods that will help you navigate.
When you create a Statement without passing parameters to it, it defaults to ResultSet.TYPE_FORWARD_ONLY.
Statement st = con.createStatement();
If you want to be able to scroll back you set the first parameter to either ResultSet.TYPE_SCROLL_INSENSITIVE or ResultSet.TYPE_SCROLL_SENSITIVE. (Insensitive or sensitive in regard to changes to the database by others.)
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
CONCUR_UPDATABLE will allow you to update the ResultSet if you want, unless you do not need to, i which case you set it to CONCUR_READ_ONLY.
Read this.
Heres a class executing PreparedStatements on a Connection.
public class doSomething {
private PreparedStatement ps;
public setPS (Connection conn) throws SQLException {
String sql = "select * from table where id = ?";
ps = conn.prepareStatement(sql);
}
public void runSomething(String var){
ps.setString(1,var);
ResultSet rs = ps.executeQuery();
...
}
}
I call
doSomethingInstance.setPS(conn);
doSomethingInstance.runSomething(var);
from another class and this throws and exception at the
ResultSet rs = ps.executeQuery();
The exception is SQLException: JZ0S4: Cannot execute an empty (zero-length) query. on a Prepared Statement. I cant understand why. Does anyone see what Iam doing wrong here?
Thanks!
Go back and copy the code straight from your source file, then edit your question. You have a potential ambiguity: Your first fragment calls the prepared statement "preparedStatement", then you change to "prepareStatement" (without the "d"). Having a clean look at your source code will make isolating the problem easier. Do you have two variables or did you mistype your example?
[later...]
Thanks for updating your code. I'm not seeing an obvious problem with it. Have you stepped through it with a debugger (e.g., in Eclipse) to ensure that the two methods are being called as expected?
This issue has been fixed folks. There was a problem with the database permissions. Catching and printing the stacktrace on
ResultSet rs = ps.executeQuery();
indicated that the table being accessed by the query was not found. I still have to figure out why the SQLException: JZ0S4: Cannot execute an empty (zero-length) query is being thrown.
Thanks for your time and consideration.
Rohan
can you print the contents of sql before executing the query? I suspect it's losing it's contents
Is var non null? Is var of the correct type(int instead of String I would think?)