Hi!
Im doing a program in java that gets info from a database.. thing is that im seting a variable with data from one table, and one data is char and cant find the way to have the .getChar() like the rs.getString()

the code is:

public ModosDeCompra_clase dameModoCompra(Integer id)
    {
        ModosDeCompra_clase mdc = new ModosDeCompra_clase();
        Statement comando;
        boolean seguir;
        ResultSet rs;

        try
        {
            comando = conexiones.cnn.createStatement();
            rs = comando.executeQuery("SELECT id_provincia,nombre_provincia FROM provincias WHERE id_provincia ="+ id);
            seguir = rs.next();

            while (seguir)
            {
                mdc.setId_mododecompra(rs.getByte(1));
                mdc.setNombre(rs.getString(2));
                mdc.setHabilitado(rs.getArray(3));
                seguir = rs.next();
            }
            return mdc;
        }
        catch(Exception e)
        {
                JOptionPane.showMessageDialog (null,e.getMessage()+"error en -dameModoCompra(Integer id)-");
            return null;
        }
    }

and i use it here:

public boolean mostrarConsulta()    // this is to show a list of the things i've stored in the db
    {
        Tipos_Insumos_Clase mdcActual;
        boolean seguir;
        ResultSet rs;
        Statement comand;

        listaTiposInsumos.clear();

        try
        {
            listTiposInsumos.removeAll();
            conexiones.conectaralabase();
            comand = conexiones.cnn.createStatement();
            rs = comand.executeQuery("SELECT id_insumo,nombre_insumo,forma_compra,habilitado FROM tipos_insumos WHERE habilitado = 1 ORDER BY nombre_insumo");

            seguir = rs.next();

            while (seguir)
            {
                mdcActual = new Tipos_Insumos_Clase();
                
                mdcActual.setId_insumo(rs.getInt(1));
                mdcActual.setNombre_insumo(rs.getString(2));
                mdcActual.setMedida_insumo(dameModoCompra(rs.getInt(3)));

                listaTiposInsumos.add(mdcActual);
                seguir = rs.next();
            }
            listTiposInsumos.setListData(listaTiposInsumos.toArray());
            return true;
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog (null,e.getMessage());
            return false;
        }
    }

thanks in advanced

i forgot to put where the problem was:

while (seguir)
{
mdc.setId_mododecompra(rs.getByte(1));
mdc.setNombre(rs.getString(2));
mdc.setHabilitado(rs.getArray(3)); // THE PROBLEM IS HERE
seguir = rs.next();
}

use rs.getString() and then convert to a char maybe

/* String ss="h";
    char cd=ss.charAt(0);//0 because a char is only a single character so get first and only letter of the string
*/
char c=rs.getString().charAt(0);

i could resolve it :D

rewritten this

while (seguir)
{
mdc.setId_mododecompra(rs.getByte(1));
mdc.setNombre(rs.getString(2));
mdc.setHabilitado(rs.getArray(3)); // THE PROBLEM IS HERE
seguir = rs.next();
}

into this:

while (seguir)
            {
                char c = (char) rs.getInt(3);
                
                mdc.setId_mododecompra(rs.getByte(1));
                mdc.setNombre(rs.getString(2));
                mdc.setHabilitado(c);
                seguir = rs.next();
            }
            return mdc;
        }

changed

mdc.setHabilitado(c);

and added

char c = (char) rs.getInt(3);

to have the variable set as char :)

thanks for the help :D

glad to help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.