|
| 1 | +{ |
| 2 | + "id": 112, |
| 3 | + "slug": "jdbc-vs-jooq", |
| 4 | + "title": "JDBC versus jOOQ", |
| 5 | + "category": "enterprise", |
| 6 | + "difficulty": "intermediate", |
| 7 | + "jdkVersion": "11", |
| 8 | + "oldLabel": "Raw JDBC", |
| 9 | + "modernLabel": "jOOQ", |
| 10 | + "oldApproach": "Raw JDBC", |
| 11 | + "modernApproach": "jOOQ SQL DSL", |
| 12 | + "oldCode": "String sql = \"SELECT id, name, email FROM users \"\n + \"WHERE department = ? AND salary > ?\";\ntry (Connection con = ds.getConnection();\n PreparedStatement ps =\n con.prepareStatement(sql)) {\n ps.setString(1, department);\n ps.setBigDecimal(2, minSalary);\n ResultSet rs = ps.executeQuery();\n List<User> result = new ArrayList<>();\n while (rs.next()) {\n result.add(new User(\n rs.getLong(\"id\"),\n rs.getString(\"name\"),\n rs.getString(\"email\")));\n }\n return result;\n}", |
| 13 | + "modernCode": "DSLContext dsl = DSL.using(ds, SQLDialect.POSTGRES);\n\nreturn dsl\n .select(USERS.ID, USERS.NAME, USERS.EMAIL)\n .from(USERS)\n .where(USERS.DEPARTMENT.eq(department)\n .and(USERS.SALARY.gt(minSalary)))\n .fetchInto(User.class);", |
| 14 | + "summary": "Replace raw JDBC string-based SQL with jOOQ's type-safe, fluent SQL DSL.", |
| 15 | + "explanation": "jOOQ (Java Object Oriented Querying) generates Java code from your database schema, turning table and column names into type-safe Java constants. The fluent DSL mirrors SQL syntax so queries are readable and composable. All parameters are bound automatically, eliminating SQL injection risk. Unlike JPA/JPQL, jOOQ embraces SQL fully — window functions, CTEs, RETURNING clauses, and vendor-specific extensions are all first-class.", |
| 16 | + "whyModernWins": [ |
| 17 | + { |
| 18 | + "icon": "🔒", |
| 19 | + "title": "Type-safe columns", |
| 20 | + "desc": "Column names are generated Java constants — typos and type mismatches become compiler errors instead of runtime failures." |
| 21 | + }, |
| 22 | + { |
| 23 | + "icon": "📖", |
| 24 | + "title": "SQL fluency", |
| 25 | + "desc": "The jOOQ DSL mirrors SQL syntax closely, so complex JOINs, subqueries, and CTEs stay readable." |
| 26 | + }, |
| 27 | + { |
| 28 | + "icon": "🛡️", |
| 29 | + "title": "Injection-free by design", |
| 30 | + "desc": "Parameters are always bound safely — no string concatenation means no SQL injection risk." |
| 31 | + } |
| 32 | + ], |
| 33 | + "support": { |
| 34 | + "state": "available", |
| 35 | + "description": "jOOQ open-source edition supports all major open-source databases; older commercial databases require a paid license" |
| 36 | + }, |
| 37 | + "prev": "enterprise/spring-xml-config-vs-annotations", |
| 38 | + "next": null, |
| 39 | + "related": [ |
| 40 | + "enterprise/jdbc-vs-jpa", |
| 41 | + "enterprise/jdbc-resultset-vs-jpa-criteria", |
| 42 | + "enterprise/manual-transaction-vs-declarative" |
| 43 | + ], |
| 44 | + "docs": [ |
| 45 | + { |
| 46 | + "title": "jOOQ — Getting Started", |
| 47 | + "href": "https://www.jooq.org/doc/latest/manual/getting-started/" |
| 48 | + }, |
| 49 | + { |
| 50 | + "title": "jOOQ — DSL API Reference", |
| 51 | + "href": "https://www.jooq.org/javadoc/latest/" |
| 52 | + } |
| 53 | + ] |
| 54 | +} |
0 commit comments