Java JDBC Lazy-Loaded ResultSet

Short answer: Use Statement.setFetchSize(1) before calling executeQuery(). Long answer: This depends very much on which JDBC driver you are using. You might want to take a look at this page, which describes the behavior of MySQL, Oracle, SQL Server, and DB2. Major take-aways: Each database (i.e. each JDBC driver) has its own default behavior. Some … Read more

How do you get values from all columns using ResultSet.getBinaryStream() in jdbc?

You can get all the column names and the entire data from your table using the code below. writeToFile method will contain the logic to writing to file (if that was not obvious enough 🙂 ) ResultSetMetaData metadata = rs.getMetaData(); int columnCount = metadata.getColumnCount(); for (int i = 1; i <= columnCount; i++) { writeToFile(metadata.getColumnName(i) … Read more

How do I get the row count in JDBC?

You’re going to have to do this as a separate query, for example: SELECT COUNT(1) FROM table_name Some JDBC drivers might tell you but this is optional behaviour and, more to the point, the driver may not know yet. This can be due to how the query is optimised eg two example execution strategies in … Read more

Statement.execute(sql) vs executeUpdate(sql) and executeQuery(sql)

boolean execute(): Executes the SQL statement in this Prepared Statement object, which may be any kind of SQL statement. ResultSet executeQuery(): Executes the SQL query in this Prepared Statement object and returns the ResultSet object generated by the query. int executeUpdate(): Executes the SQL statement in this Prepared Statement object, which must be an SQL … Read more

Bulk insert in Java using prepared statements batch update

I’ll address your questions in turn. Will the executeBatch method tries to send all the data at once? This can vary with each JDBC driver, but the few I’ve studied will iterate over each batch entry and send the arguments together with the prepared statement handle each time to the database for execution. That is, … Read more

Getting the date from a ResultSet for use with java.time classes

Most database vendors don’t support JDBC 4.2 yet. This specification says that the new java.time-types like LocalDate will/should be supported using the existing methods setObject(…) and getObject(). No explicit conversion is required and offered (no API-change). A workaround for the missing support can be manual conversion as described on the Derby-mailing list. Something like: LocalDate … Read more

When to close Connection, Statement, PreparedStatement and ResultSet in JDBC

Personally I’d use a pool as this will take care of all of the resource management for you. If your connection requirements change then you can easily modify the pool configuration. With a pool in place you can open/close connections and prepared statements according to best-practice and leave the resource management to the pool. Typically, … Read more

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