How to find whether a ResultSet is empty or not in Java? [duplicate]
Immediately after your execute statement you can have an if statement. For example ResultSet rs = statement.execute(); if (!rs.next()){ //ResultSet is empty }
Immediately after your execute statement you can have an if statement. For example ResultSet rs = statement.execute(); if (!rs.next()){ //ResultSet is empty }
This is a bad idea. This approach requires that the connection is open the whole time until the last row is read, and outside the DAO layer you never know when it will happen, and you also seem to leave the resultset open and risk resource leaks and application crashes in the case the connection … Read more
If you don’t want to use any JPA provider such as OpenJPA or Hibernate, you can just give Apache DbUtils a try. http://commons.apache.org/proper/commons-dbutils/examples.html Then your code will look like this: QueryRunner run = new QueryRunner(dataSource); // Use the BeanListHandler implementation to convert all // ResultSet rows into a List of Person JavaBeans. ResultSetHandler<List<Person>> h = … Read more
Use: Array a = rs.getArray(“is_nullable”); String[] nullable = (String[])a.getArray(); As explained here Array is SQL type, getArray() returns an object to cast to java array.
I didn’t test it, but why wouldn’t it work? new Iterator[String] { def hasNext = resultSet.next() def next() = resultSet.getString(1) }.toStream
You just can put your query as a subquery: SELECT avg(count) FROM ( SELECT COUNT (*) AS Count FROM Table T WHERE T.Update_time = (SELECT MAX (B.Update_time ) FROM Table B WHERE (B.Id = T.Id)) GROUP BY T.Grouping ) as counts Edit: I think this should be the same: SELECT count(*) / count(distinct T.Grouping) FROM … Read more
JDBC will simply name the columns by what is specified in the query – it doesn’t know about table names etc. You have two options: Option 1: Name the columns differently in the query, ie SELECT a.columnName as columnNameA, b.columnName as columnNameB, … from table1 a, table2 b where (WHATEVER) then in your java code … Read more
This answer is outdated. Continue to Basil Bourque’s answer. java.util.Date date; Timestamp timestamp = resultSet.getTimestamp(i); if (timestamp != null) date = new java.util.Date(timestamp.getTime())); Then format it the way you like.
Warning: I’m going to get bombastic here, because this drives me crazy. 99%* of the time, it’s a ridiculous micro-optimization that people have some vague idea makes things ‘better’. This completely ignores the fact that, unless you’re in an extremely tight and busy loop over millions of SQL results all the time, which is hopefully … Read more
There’s a dummy-table in MySQL called ‘dual’, which you should be able to use. select 1 from dual where false This will always give you an empty result.