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).
These methods exist in my connection 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:
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:
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.
CodePudding user response:
loadOrderList
calls (indirectly) loadItemList
which changes the st
and rs
fields while rs
is still needed in loadOrderList
for the next iterations.