-1

I have created a method (getData()) to receive data from the database which seems to work. What I want is to use only 1 parameter, so the method should run with className.getData("SELECT * FROM myTable"). How can I do this?

public List<String> getData(String sql, Map column) {
    this.driver();
    try {
        Connection conn = DriverManager.getConnection(this.url);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);

        List<String> result = new ArrayList<>();
        if (column.containsKey("string")) {
            for (Object i : column.values()) {
                while (rs.next()) {
                    result.add(rs.getString(String.valueOf(i)));
                }
            }
            return result;
        }

    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

private void driver() {
    // Ensure we have mariadb Driver in classpath
    try {
        Class.forName("org.mariadb.jdbc.Driver");
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Ray
  • 125
  • 3
  • 12
  • 6
    I'm voting to close this question as off-topic because it is about code review an should be asked at [codereview.se] – Jens Jul 05 '19 at 19:59
  • 1
    `Map` is using a raw type which you should endeavor to avoid. A Map is also a set of *mappings* from `X -> Y`. I can't see how such a structure can possibly represent a column... If you can't think of a name such as `fooToBar`, a map is almost certainly wrong: `employeeToSalary`, `personToAge`, `isbnToBook`. `column`? No. – Michael Jul 05 '19 at 20:00
  • I'm confused. `SELECT * FROM myTable` returns many rows with many columns. How is that supposed to be returned as a `List`? Should it be a `List>` or some other 2D structure? – Andreas Jul 05 '19 at 20:10

2 Answers2

4

For sure you don't need pass the column name as parameter. JDBC allow you get column name from metaData. Just like below

ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
Integer colunNum = rsmd.getColumnCount();
for(int i =0; i < columnNum; i ++){
    String colmnName = rsmd.getColumnName(i);
    ....
}

credit to: Retrieve column names from java.sql.ResultSet

Qingfei Yuan
  • 1,196
  • 1
  • 8
  • 12
0

Call the driver method in the class instantiation.

static {
    // Ensure we have mariadb Driver in classpath
    try {
        Class.forName("org.mariadb.jdbc.Driver");
    }
    catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

Also you should be closing your resources. You can do this with a try-with-resources.

public List<String> getData(String sql, Map column) {
    try (
        Connection conn = DriverManager.getConnection(this.url);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
     ){
        List<String> result = new ArrayList<>();
        if (column.containsKey("string")) {
            for (Object i : column.values()) {
                while (rs.next()) {
                    result.add(rs.getString(String.valueOf(i)));
                }
            }
            return result;
        }

    }
    catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
patstuart
  • 1,931
  • 1
  • 19
  • 29