title: How to pull BLOB(from 2d array to BLOB) from mySQL database using JAVA
body: <p>The code below is for converting and inserting 2darrays into BLOB into the database</p>
  
  <pre><code>    ByteArrayOutputStream bas = new ByteArrayOutputStream();
      DataOutputStream ds = new DataOutputStream(bas);
      for (float f : secondArray) 
          ds.writeFloat(f);
      byte[] bytes = bas.toByteArray();
      java.sql.Blob b1 = new SerialBlob(bytes);
  
      PreparedStatement state = conn.prepareStatement("INSERT INTO blob_table (id, for_blob) " + " VALUES(6,?)");
      state.setBlob(1, b1);
      state.executeUpdate();
  </code></pre>
  
  <p>The code below is the one for extracting the BLOB and converting it back to 2d array </p>
  
  <pre><code>Statement state1 =  conn.createStatement();
      ResultSet rs = state1.executeQuery("SELECT * FROM blob_table WHERE id = 6");
  
      while(rs.next()){
          try{
              Blob blob1 = rs.getBlob(1);
              byte[] bytesInBlob = blob1.getBytes(1, (int) blob1.length());
  
              ByteArrayInputStream bas1 = new ByteArrayInputStream(bytesInBlob);
              DataInputStream dis = new DataInputStream(bas1);
              float[] fArr = new float[bytesInBlob.length / 4];  // 4 bytes per float
              for (int i = 0; i &lt; fArr.length; i++)
              {
                  fArr[i] = dis.readFloat();
              }
              for(int i=0;i&lt;fArr.length;i++)
                  System.out.println(fArr[i]);            
  
          }catch(Exception i){
              i.printStackTrace();
          }
      }
  </code></pre>
  
  <p>But this code gives me the wrong numbers from the 2d array.</p>