简体   繁体   English

Retrieve data from MySQL in Java (After end of result set error)

[英]Retrieve data from MySQL in Java (After end of result set error)

Can anyone tell me what is wrong with my code here, and what is the best approach to do the same thing?谁能告诉我这里的代码有什么问题,做同样事情的最佳方法是什么?

I'm trying to connect to a database and retrieve a list of unprocessed orders, recreate the order object which takes as parameters, a customer object, a product object, and an integer (unity).我正在尝试连接到数据库并检索未处理订单的列表,重新创建订单 object 作为参数,客户 object,产品 object 和 integer(统一)。

These methods exist in my connection class:这些方法存在于我的连接 class 中:

public Map<String, Cliente> loadCustomerList() throws SQLException {
    conexion = DriverManager.getConnection(this.url, this.user, this.password);
    Map<String, Cliente> listaClientes = new HashMap<>();
    query = "SELECT * FROM cliente";
    st = conexion.prepareStatement(query);
    rs = st.executeQuery();
    while (rs.next()) {
        String dni = rs.getString(1);
        String nombre = rs.getString(2);
        String apellidos = rs.getString(3);
        int descuento = rs.getInt(4);

        listaClientes.put(dni, new Cliente(dni, nombre, apellidos, descuento));
    }

    //conexion.close();
    return lista Clientes;
}

public List<Item> loadItemList() throws SQLException {
    conexion = DriverManager.getConnection(this.url, this.user, this.password);
    List<Item> listaItems = new ArrayList<>();
    query = "SELECT * FROM item;";
    st = conexion.prepareStatement(query);
    rs = st.executeQuery();
    while (rs.next()) {
        String nombre = rs.getString(1);
        double precio = rs.getDouble(2);
        int existencias = rs.getInt(3);

        listaItems.add(new Item(nombre, precio, existencias));
    }

    //conexion.close();
    return listaItems;
}

public Cliente findClient(String dni) throws SQLException {
    Cliente cliente = null;
    for (Map.Entry<String, Cliente> entry : loadCustomerList().entrySet()) {
        if(entry.getKey().equals(dni)) {
            cliente = entry.getValue();
        }
    }
    return cliente;
}
    
public Producto findItem(String nombreProduco) throws SQLException {
    Producto producto = null;
    for(Producto item : loadListItems()) {
        if(item.getNombre().equals(nombreProduco)) {
            producto = item;
        }
    }
    return producto;
}

public List<Pedido> loadOrderList() throws SQLException {
    conexion = DriverManager.getConnection(this.url, this.user, this.password);
    List<Pedido> listaPedidos = new ArrayList<>();
    Cliente cliente =null;
    Producto producto =null;
    query = "SELECT * FROM pedido WHERE `Numero Factura` IS NULL";
    st = conexion.prepareStatement(query);
    rs = st.executeQuery();
    while (rs.next()) {
        int unidad = rs.getInt(3);
         producto = findItem(rs.getString(2));
         cliente = findClient(rs.getString(6));
         listaPedidos.add(new Pedido(producto, unidad, cliente));
    }
    
    conexion.close();
    
    return listaPedidos;
}

I want to be able to call the loadOrderList() method in the constructor of the main operation class like this:我希望能够在主操作 class 的构造函数中调用 loadOrderList() 方法,如下所示:

public class PruebaComercio {

    private Map<String, Cliente> listaClientes;
    private List<Pedido> listaPedidos;
    private List<Factura> listaFacturas;
    private Almacen almacen;
    private ConnectionMysql connection = new ConnectionMysql();

    public PruebaComercio() throws SQLException {
        this.listaClientes = connection.cargarListaClientes();
        this.listaPedidos = connection.loadOrderList();
        this.listaFacturas = new ArrayList<>();
        this.almacen = new Almacen();
    }

Output: Output:

Exception in thread "main" java.sql.SQLException: After end of result set
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:532)
    at com.mysql.cj.jdbc.result.ResultSetImpl.getString(ResultSetImpl.java:878)
    at Facturacion.ConnectionMysql.cargarListaPedido(ConnectionMysql.java:166)
    at Facturacion.PruebaComercio.<init>(PruebaComercio.java:23)
    at Facturacion.Main.main(Main.java:25)

Sorry for the use of Spanish here, the project was done in Spanish, I did my best to translate the important things.抱歉在这里使用西班牙语,该项目是用西班牙语完成的,我尽力翻译了重要的事情。

loadOrderList calls (indirectly) loadItemList which changes the st and rs fields while rs is still needed in loadOrderList for the next iterations. loadOrderList调用(间接) loadItemList ,它更改strs字段,而loadOrderList中仍然需要rs用于下一次迭代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM