编写java mysql以使用hashmap和while循环检索数据

ati*_*ain 2 java jdbc

我不知道如何使用hashmap和while循环从我的数据库中检索数据.请帮助我.
我的代码是

    package com.glomindz.mercuri.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.HashMap;

    import com.glomindz.mercuri.util.MySingleTon;

          public class UserServicesDAO {

private Connection connection;

public UserServicesDAO() {
    //connection = new MySingleTon().getConnection();
    connection = MySingleTon.getInstance().getConnection();

}

public void get_all_data() {
    HashMap<Integer, String> result = new HashMap<Integer, String>();
    String query = "SELECT * FROM spl_user_master";
    try {
        PreparedStatement stmt = connection.prepareStatement(query);
        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    new UserServicesDAO().get_all_data();
}
 }
Run Code Online (Sandbox Code Playgroud)

代码有什么问题?

jue*_*n d 6

您可以在这样的循环中从记录中获取值

ResultSet resultSet = stmt.getResultSet();
while (resultSet.next()) {
    String someStringValue = resultSet.getString("some_column_name");
    int someIntegerValue = resultSet.getInt("some_other_column_name");
    //...
}
Run Code Online (Sandbox Code Playgroud)