Can a JPA Query return results as a Java Map?

Returning a Map result using JPA Query getResultStream Since the JPA 2.2 version, you can use the getResultStream Query method to transform the List<Tuple> result into a Map<Integer, Integer>: Map<Integer, Integer> postCountByYearMap = entityManager.createQuery(“”” select YEAR(p.createdOn) as year, count(p) as postCount from Post p group by YEAR(p.createdOn) “””, Tuple.class) .getResultStream() .collect( Collectors.toMap( tuple -> ((Number) … Read more

Get Number of Rows returned by ResultSet in Java

First, you should create Statement which can be move cursor by command: Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); Then retrieve the ResultSet as below: ResultSet rs = stmt.executeQuery(…); Move cursor to the latest row and get it: if (rs.last()) { int rows = rs.getRow(); // Move to beginning rs.beforeFirst(); … } Then rows variable will contains … Read more

How can I determine if the column name exist in the ResultSet?

Use the ResultSetMetaData class. public static boolean hasColumn(ResultSet rs, String columnName) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); int columns = rsmd.getColumnCount(); for (int x = 1; x <= columns; x++) { if (columnName.equals(rsmd.getColumnName(x))) { return true; } } return false; } The thing I don’t understand is why this function would ever be needed. … Read more

How to get row count using ResultSet in Java?

If you have access to the prepared statement that results in this resultset, you can use connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); This prepares your statement in a way that you can rewind the cursor. This is also documented in the ResultSet Javadoc In general, however, forwarding and rewinding cursors may be quite inefficient for large result sets. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)