Home » Java » java – How can I print or put to some HashMap or anything else my result of query?-Exceptionshub

java – How can I print or put to some HashMap or anything else my result of query?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have an SQL query in the following Java Code:

try {
    Statement st = conn.createStatement();
    try {
        st.execute("SELECT * FROM students WHERE course_id =1");
    } finally {
        // st.close();
    }
} catch (SQLException ex) {
    throw new RuntimeException(ex);
}

When I execute this code, I don’t see anything in the console.

I want to put the results to a Hashmap or print them to see the values, at least.

How can I implement printing to the console or storing the results in a Map?

How to&Answers:

It would really help to know the column information (labels, amount, types), but maybe, you can do what you want having the following code as a hint or template and reading the comments carefully:

public static void main(String[] args) {
    // provide the query as String with a question mark for the parameter
    String query = "SELECT * FROM students WHERE course_id = ?";
    // and provide the parameter value
    int id = 1;

    // use a try with resources in order to automatically close the resources
    try (PreparedStatement ps = conn.prepareStatement(query)) { // WON'T COMPILE due to conn
        // set the position and the parameter to the query
        ps.setInt(1, id);
        // execute the query and receive the results
        ResultSet rs = ps.executeQuery();

        /*
         *  since we neither know how many columns "* FROM students" returns, 
         *  nor what types their values have, we have to determine them programmatically 
         */

        // that means we need to receive them from the meta data of the result set
        ResultSetMetaData rsmd = rs.getMetaData();

        // provide a StringBuilder for the output of the header information
        StringBuilder headerInfoBuilder = new StringBuilder();

        // extract the column information
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            headerInfoBuilder.append(rsmd.getColumnLabel(i))
                            .append(" [")
                            .append(rsmd.getColumnType(i))
                            .append("]")
                            .append(";");
        }

        // remove the last semicolon
        String headerInfo = headerInfoBuilder.toString()
                                            .substring(0, headerInfoBuilder.length() - 1);
        // and print the header information
        System.out.println(headerInfo);

        // provide a variable that counts the rows
        int rowCount;

        // then iterate the results
        while (rs.next()) {
            /*
             * print the results here or store them in a data structure,
             * depends on the types and amount of columns
             */

            rowCount++;
        }

        // output the row count
        System.out.println(rowCount + " rows received");

    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

Answer:

Try this :-

     try {
            Statement st = conn.createStatement();
            try {
                ResultSet rs=st.executeQuery("SELECT * FROM students WHERE course_id =1");
                while(rs.next()) {
                    //Your logic to put in map goes here
                }
            } finally {
               // st.close();
            }
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }