Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda