简体   繁体   中英

How do I convert STRING to VARBINARY in java?

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!

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)

You can try like this using AppacheCommon :

String str = "someSting";
byte[] b = Base64.decodeBase64(str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM