Good afternoon, I have a code where depending on the product chosen is the number of pictograms that the label of this product must have, the class where I consult that table depending on the product ID is as follows:
private int id_pictograma;
private int activo;
public Vector<ConsultasPictogramas> MostrarPictograma(int a) {
PreparedStatement ps = null;
ResultSet rs = null;
Conexion conn = new Conexion();
Connection con = conn.getConnection();
Vector<ConsultasPictogramas> datos = new Vector<ConsultasPictogramas>();
ConsultasPictogramas dat = null;
try {
String sql = "SELECT Id_pictograma,activo FROM k_producto_pictograma WHERE Id_producto=" + a;
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
dat = new ConsultasPictogramas();
while (rs.next()) {
dat = new ConsultasPictogramas();
dat.setId_pictograma(rs.getInt("Id_pictograma"));
dat.setActivo(rs.getInt("activo"));
datos.add(dat);
}
rs.close();
} catch (SQLException ex) {
System.out.println(ex.toString());
}
return datos;
}
and in a jframe I command to call the method to use the data that it is supposed to have
String id = CBProductos.getSelectedItem().toString();
jProductoetiqueta.setText(id);
int idp=CBProductos.getSelectedIndex();
System.out.println("id=producto"+idp);
ConsultasPictogramas cc = new ConsultasPictogramas();
System.out.println("vector"+cc.MostrarPictograma(idp));
but when printing it to the console this is output:
id=producto1
vector[Controlador.ConsultasPictogramas@4b977766]
even if the product has more than two pictograms it comes out that way but twice
id=producto12
vector[Controlador.ConsultasPictogramas@7a42675e, Controlador.ConsultasPictogramas@2be8bc92]
ejemplo de producto con 4 pictogramas
id=producto24
vector[Controlador.ConsultasPictogramas@ba743a7, Controlador.ConsultasPictogramas@3366c995]
array[Controlador.ConsultasPictogramas@44be38d1, Controlador.ConsultasPictogramas@44be38d1]
What could it be ?
what happens is that you only print its reference in memory, your line of code should be something like this:
where index is the position of the Vector you want to get.
Another example:
In this case, the values of the system would be the following:
Now if you don't want to do a get on your System line, then you would have to override the toString method of your class:
Example:
In the previous class we add the toString in the test class:
With that, instead of printing the reference in memory, it would print the value that you want to show in the toString() method.