When inserting records into SQL databases, we often want to fetch back generated IDs and possibly other trigger, sequence, or default generated values. Let's assume we have the following table: -- DB2 CREATE TABLE x ( i INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, j VARCHAR(50), k DATE DEFAULT CURRENT_DATE ); -- PostgreSQL CREATE TABLE … Continue reading How to Run a Bulk INSERT .. RETURNING Statement With Oracle and JDBC
Category: java
This category contains posts about things that are essentially Java-related
Mocking JDBC Using a Set of SQL String / Result Pairs
In a previous blog post, I've shown how the programmatic MockDataProvider can be used to mock the entire JDBC API through a single functional interface: // context contains the SQL string and bind variables, etc. MockDataProvider provider = context -> { // This defines the update counts, result sets, etc. // depending on the context … Continue reading Mocking JDBC Using a Set of SQL String / Result Pairs
A Completely Overhauled, Modularised jOOQ 3.11, Ready for Java 11
If you've been following the fast paced JDK 9+ projects, you may have noticed an exciting, first big change that has been made possible thanks to Java 9's Jigsaw feature. In JDK 11, JEP 320 will ship, or rather: it will no longer ship, as JEP 320 means that both CORBA and Java EE modules … Continue reading A Completely Overhauled, Modularised jOOQ 3.11, Ready for Java 11
Correct Reflective Access to Interface Default Methods in Java 8, 9, 10
When performing reflective access to default methods in Java, Google seems to fail us. The solutions presented on Stack Overflow, for instance, seem to work only in a certain set of cases, and not on all Java versions. This article will illustrate different approaches to calling interface default methods through reflection, as may be required … Continue reading Correct Reflective Access to Interface Default Methods in Java 8, 9, 10
Map Reducing a Set of Values Into a Dynamic SQL UNION Query
Sounds fancy, right? But it's a really nice and reasonable approach to doing dynamic SQL with jOOQ. This blog post is inspired by a Stack Overflow question, where a user wanted to turn a set of values into a dynamic UNION query like this: SELECT T.COL1 FROM T WHERE T.COL2 = 'V1' UNION SELECT T.COL1 … Continue reading Map Reducing a Set of Values Into a Dynamic SQL UNION Query
How to Order Versioned File Names Semantically in Java
In most cases, natural sorting by sorting lexicographically is useful as a default in Java. This includes sorting file names, which are sorted lexicographically as well. However, when we have version numbers in our files (such as a set of SQL migration scripts), then we prefer the files to be sorted in a more intuitive … Continue reading How to Order Versioned File Names Semantically in Java
Type Safe Implicit JOIN Through Path Navigation in jOOQ 3.11
One of the biggest contributors to SQL syntax verbosity is the need to explicitly JOIN every table that somehow contributes to the query, even if that contribution is "trivial". When looking at the Sakila database, an example could be seen easily when fetching customer data: SELECT cu.first_name, cu.last_name, co.country FROM customer AS cu JOIN address … Continue reading Type Safe Implicit JOIN Through Path Navigation in jOOQ 3.11
How to Implement Your Own XJC Plugin to Generate toString(), equals(), and hashCode() Methods
When you work with JAXB to generate Java code from XSD (or *ghasp* WSDL) files, you're going to use the XJC tool, which is shipped with the JDK (at least until JDK 9 - with Jigsaw, it will soon be externalised into its own external dependency). Adding plugins to XJC when running it via Maven … Continue reading How to Implement Your Own XJC Plugin to Generate toString(), equals(), and hashCode() Methods
How to Fetch Oracle DBMS_OUTPUT from JDBC
When working with Oracle stored procedures, it is not uncommon to have debug log information available from DBMS_OUTPUT commands. For instance, if we have a procedure like this: CREATE TABLE my_table (i INT); CREATE OR REPLACE PROCEDURE my_procedure (i1 INT, i2 INT) IS BEGIN INSERT INTO my_table SELECT i1 FROM dual UNION ALL SELECT i2 … Continue reading How to Fetch Oracle DBMS_OUTPUT from JDBC
A Common Mistake Developers Make When Caching Nullable Values
Caching is hard in various ways. Whenever you're caching things, you have to at least think of: Memory consumption Invalidation In this article, I want to show a flaw that often sneaks into custom cache implementations, making them inefficient for some execution paths. I've encountered this flaw in Eclipse, recently. What did Eclipse do wrong? … Continue reading A Common Mistake Developers Make When Caching Nullable Values