Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
How to retrieve array from mysql using jdbc?
public static void main(String[] args) {

    byte[] a1 = "stack".getBytes();  //**I need to insert this array a1 into mysql table and retrieve it** 

    for (byte b : a1) {
        System.out.println(b); //**just printing the array a1**
    }
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Connection conn = null;
    try {

        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
        Statement stmt = conn.createStatement();
        String query = "INSERT INTO keytable (name, `key`) VALUES (?,?)";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "Nil");
        pstmt.setBytes(2, a1);  //**INSERTING a1**
        pstmt.execute();
        String sql = "SELECT 'key' FROM keytable";
        byte[] a2 = null;
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        a2 = rs.getBytes(1); //copying the retrieved array into a2
        for (byte b : a2) {
            System.out.print(b);  //values in array a1 and a2 are not the same (trying to fix this)
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
    System.out.println("\ndone :)");

}

I need the same array to be retrieved from the database,not able to figure out the problem.how do I fix this ?

This is the table I created in mysql

create table keytable (
n1 varchar(255) not null, 
'key' blob not null); 
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda