title: JdbcTemplate: accessing MySQL VARBINARY field as String
body: <p>I'm having trouble reading MySQL's <strong>VARBINARY</strong> field as a String using <em>JdbcTemplate</em>. We're storing string abbreviations ("ABC", "XYZ", "LMN" and the like) as VARBINARYs (don't ask me why). Strangely enough, when I use the Connection class / PreparedStatement route, and plain old ResultSets vs. SqlRowSet, I have no problem reading the String. That is to say,</p>
  
  <p>This code works:</p>
  
  <pre><code>String sql = "select MY_VARBINARY_FIELD from MY_TABLE where KEY1=? and KEY2=?";
  PreparedStatement stmt = connectionDev.prepareStatement(sql);
  prepStmt1.setInt(1, key1);
  prepStmt1.setInt(2, key2);
  ResultSet rs = stmt.executeQuery();
  
  while (rs.next()) {
      String s = rs.getString("MY_VARBINARY_FIELD");
      System.out.print(s + " ");
  }
  
  **Output:** AHI-1 DKFZp686J1653 FLJ14023 FLJ20069 JBTS3 ORF1 dJ71N10.1 
  </code></pre>
  
  <p>But this code doesn't:</p>
  
  <pre><code>String sql = "select MY_VARBINARY_FIELD from MY_TABLE where KEY1=? and KEY2=?";
  Object[] params = {key1, key2};
  SqlRowSet rows = getJdbcTemplate().queryForRowSet(sql, params);
  
  while (rows.next()) {
      String s = rows.getString("MY_VARBINARY_FIELD");
      System.out.print(s + " ");
  }
  
  **Output:** [B@3a329572 [B@4ef18d37 [B@546e3e5e [B@11c0b8a0 [B@399197b [B@3857dc15 [B@10320399 
  </code></pre>
  
  <p>Why do SqlRowSet and ResultSet produce a different String representation for the VARBINARY? And how can I get the "correct" representation using JdbcTemplate/SqlRowSet?</p>
  
  <p>Thanks! </p>
  
  <p><strong>SOLUTION</strong></p>
  
  <p>Mark Rotteveel (below) answered the question. I got it to work with this:</p>
  
  <pre><code>String sql = "select MY_VARBINARY from MY_TABLE where KEY=VALUE";
  
  SqlRowSet rows = getJdbcTemplate().queryForRowSet(sql);
  
  while (rows.next()) {
       byte[] varbinary = (byte[]) rows.getObject("MY_VARBINARY");
       System.out.println(new String(varbinary));
  }
  </code></pre>