Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!