The jOOQ User Manual : Getting started with jOOQ : Tutorials : jOOQ in 7 easy steps : Step 6: Iterating | previous : next |
New versions: Dev (3.15) | Latest (3.14) | 3.13 | 3.12 | 3.11 | 3.10 | 3.9 | 3.8 | Old versions: 3.7 | 3.6 | 3.5 | 3.4 | 3.3 | 2.6
Step 6: Iterating
Applies to ✅ Open Source Edition ✅ Express Edition ✅ Professional Edition ✅ Enterprise Edition
After the line where we retrieve the results, let's iterate over the results and print out the data:
for (Record r : result) { Long id = r.getValue(POSTS.ID); String title = r.getValue(POSTS.TITLE); String description = r.getValue(POSTS.BODY); System.out.println("ID: " + id + " title: " + title + " desciption: " + description); }
The full program should now look like this:
package test; // For convenience, always static import your generated tables and // jOOQ functions to decrease verbosity: import static test.generated.Tables.*; import static org.jooq.impl.Factory.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.jooq.Record; import org.jooq.Result; import test.generated.GuestbookFactory; import test.generated.tables.Posts; public class Main { /** * @param args */ public static void main(String[] args) { Connection conn = null; String userName = "root"; String password = ""; String url = "jdbc:mysql://localhost:3306/guestbook"; try { Class.forName("com.mysql.cj.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, userName, password); Factory create = new Factory(conn, SQLDialect.MYSQL); Result<Record> result = create.select().from(POSTS).fetch(); for (Record r : result) { Long id = r.getValue(POSTS.ID); String title = r.getValue(POSTS.TITLE); String description = r.getValue(POSTS.BODY); System.out.println("ID: " + id + " title: " + title + " desciption: " + description); } } catch (Exception e) { // For the sake of this tutorial, let's keep exception handling simple e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException ignore) { } } } } }
Feedback
Do you have any feedback about this page? We'd love to hear it!