Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何打印或将查询结果放入HashMap或其他任何内容?_Java_Sql - Fatal编程技术网

Java 如何打印或将查询结果放入HashMap或其他任何内容?

Java 如何打印或将查询结果放入HashMap或其他任何内容?,java,sql,Java,Sql,我在以下Java代码中有一个SQL查询: try { Statement st = conn.createStatement(); try { st.execute("SELECT * FROM students WHERE course_id =1"); } finally { // st.close(); } } catch (SQLException ex) { throw new RuntimeException(e

我在以下Java代码中有一个SQL查询:

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);
}
当我执行这段代码时,在控制台中看不到任何东西

我想把结果放到Hashmap或打印出来,至少可以看到值

如何实现向控制台打印或将结果存储在地图中?

尝试以下方法:-

     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);
        }

了解列信息标签、数量、类型确实会有帮助,但也许您可以使用以下代码作为提示或模板,并仔细阅读注释:

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);
    }
}

请看一看,使用ResultSet rs=st.executequery代替您的查询。然后循环返回的结果集。你能告诉我们查询返回的列是什么,列值有什么类型吗?我想你不需要第二次尝试,也应该在最后,在catch之后:好的,我怎样才能知道我收到的这个查询的行的大小?