I have a question. Someone recently told me to use JOOQ if I interact with databases, so I have been reforming my code to work properly with JOOQ, but I am struggling with the way, how I select data from the database and return it. I have the following SqlConn
class:
package com.company.database;
import org.jooq.DSLContext;
import org.jooq.Result;
import org.jooq.Record;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.sql.*;
import java.util.HashMap;
public class SqlConn {
private final String SERVER = "database.me:3306";
private final String DATABASE = "testdb";
private final String USERNAME = "test";
private final String PASSWORD = "test";
private final String URL = "jdbc:mysql://" + SERVER + "/" + DATABASE;
public HashMap<String, String> getMarketCoins() {
HashMap<String, String> returnValue = new HashMap<>();
try (Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
Result<Record> result = create.select().from("MarketCoins").fetch();
for (Record r : result) {
returnValue.put(r.getValue("Market").toString(), r.getValue("Coin").toString());
}
}
catch (Exception e) {
e.printStackTrace();
}
return returnValue;
}
}
Now this code works like it should, but this is just a function that collects data from 1 specific table. My previous code was build that I could provide a Query and the code would execute the query and return the result, but with the new JOOQ method, it seems that I need to create a function for each different query and table. I watched this beginner tutorial about JOOQ: https://www.youtube.com/watch?v=4H3AGK_hNMA, but in this video he also creates multiple functions for different database calls. Is this correct, or is there a better way to easily get data from my database, where it doesn't matter for which table? I have a lot of tables in my database, so that would mean that I need a lot of function writing :(
Please let me know!