2

I have a String containing XML tags that I want to convert to VARBINARY and post it to stored procedure in SQL Server.

I don't know how to convert a String to VARBINARY. Please advise!

TT.
  • 15,774
  • 6
  • 47
  • 88
ERP
  • 41
  • 1
  • 1
  • 10

2 Answers2

6

If you have a PreparedStatement you should be able to do something like this:

// This is your input string
String value = "SomeValue";

// Your query goes here
PreparedStatement s = connection.prepareStatement(
    "UPDATE TheTable SET XmlField = ? WHERE Id = ?");

// Convert the input string to bytes according to the UTF-8 character encoding
byte[] varBinary = value.getBytes(StandardCharsets.UTF_8);

// Set the XmlField parameter in the prepared statement.
s.setBytes(1, varBinary)

// ID field
s.setInt(2, 42)
Henrik Gustafsson
  • 51,180
  • 9
  • 47
  • 60
1

You can try like this using AppacheCommon:

String str = "someSting";
byte[] b = Base64.decodeBase64(str);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331