【问题标题】:How do I convert STRING to VARBINARY in java?如何在 Java 中将 STRING 转换为 VARBINARY?
【发布时间】:2016-02-09 11:27:12
【问题描述】:

我有一个包含 XML 标记的 String,我想将其转换为 VARBINARY 并将其发布到 SQL Server 中的存储过程。

我不知道如何将String 转换为VARBINARY。请指教!

【问题讨论】:

    标签: java sql-server jdbc varbinary


    【解决方案1】:

    如果你有PreparedStatement,你应该可以这样做:

    // 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)
    

    【讨论】:

      【解决方案2】:

      你可以这样尝试使用AppacheCommon:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-26
        • 1970-01-01
        • 2010-09-19
        • 2015-11-17
        • 2011-12-03
        • 2011-07-31
        相关资源
        最近更新 更多