0

This is my first time using Java and i got confused.

I created this method:

List<Map<String, Object>>resultSet= getResultInMapList(urlString, driverr, usernameString, password, sqlQuery, "323");

which pass in a harcoded value, in this case it is 323, right?

My getResultInMapList looks like this:

public static List<Map<String, Object>> getResultInMapList(String urlString, String driverr, String usernameString, String password,String sqlQuery, Object... params) throws
    SQLException, IOException {

        try {
            createConn(urlString,driverr,usernameString,password);
            if (params == null) {
            return run.query(conn, sqlQuery, new MapListHandler());
      } else {
          return run.query(conn, sqlQuery, new MapListHandler(), params);
    }
     } catch (SQLException se) {
    se.printStackTrace();
         return null;
     } finally {
    closeConn();
     }
   }

The problem is that i don't want to pass a harcoded value anymore, instead of that i want to pass the result of a SQL Query.

SQL Query:

sqlQuery2: "select name from fake_table" (it is just an example)

Expected result:

List<Map<String, Object>>resultSet= getResultInMapList(urlString, driverr, usernameString, password, sqlQuery, sqlQuery2);

The problem is that i don't know how to modify my getResultInMapList in order to make it work.

I tried doing something like this:

public static List<Map<String, Object>> getResultInMapList(String urlString, String driverr, String usernameString, String password,String sqlQuery, sqlQuery2) 

but then the "params" does not exist anymore

What should i do in order to make it work?

JustToKnow
  • 785
  • 6
  • 23

1 Answers1

1

Run sqlQuery2 first, store the results in a different List, then convert the List to an array:

List<Object> paramList = new ArrayList<>();

try (Statement statement = conn.createStatement()) {
    ResultSet results = statement.executeQuery(sqlQuery2);
    while (results.next()) {
        paramList.add(results.getString(1));
    }
}

Object[] params = paramList.toArray();

Whether results.getString(1) is actually the right column to retrieve will depend on your database and your query. I assume sqlQuery2 only retrieves the data it needs to retrieve.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • Hey pal, thanks for replying! Which parameter should i pass? Object[] params? – JustToKnow Nov 10 '22 at 19:45
  • I mean, in here: This is my first time using Java and i got confused. List>resultSet= getResultInMapList(urlString, driverr, usernameString, password, sqlQuery, xxxx) – JustToKnow Nov 10 '22 at 19:46
  • Yes, the `params` in the above code can be used exactly as you’ve been using `params` in your existing code. – VGR Nov 10 '22 at 20:00
  • Should i import Statement and ResultSet from java.sql, right? – JustToKnow Nov 10 '22 at 20:06
  • 1
    Correct, they are classes in the java.sql package. – VGR Nov 10 '22 at 20:06
  • I'm checking this out, pal. Sorry, i'm kinda new to Java and everything seems different :) – JustToKnow Nov 10 '22 at 20:10
  • I'm getting this: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "package.Product.conn" is null at package.Product.main(Product.java:129) – JustToKnow Nov 10 '22 at 20:28
  • If `conn` is null, none of your other code will work either. You have to assign a value to `conn`. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it for details. – VGR Nov 10 '22 at 20:29
  • it is working above, i have connected to the database :/ – JustToKnow Nov 10 '22 at 20:41
  • I think i have to specifiy something like this: createConn(urlString,driverr,usernameString,password); otherwise conn in main is null – JustToKnow Nov 10 '22 at 20:56
  • i solved it by adding createConn(urlString, driverr, usernameString, password); :) – JustToKnow Nov 11 '22 at 01:02