【问题标题】:Convert java.lang.String to java.sql.Blob using Struts2 and Hibernate Framework使用 Struts2 和 Hibernate 框架将 java.lang.String 转换为 java.sql.Blob
【发布时间】:2013-01-26 11:44:36
【问题描述】:

有没有办法将 java.lang.String 转换为 java.sql.Blob ?

请帮助我,这是我的项目要求之一需要将用户密码作为 Blob 格式存储到数据库中(oracle10g)

提前致谢!

【问题讨论】:

    标签: oracle hibernate jakarta-ee struts2 blob


    【解决方案1】:

    我找到了这个例子。我看到你使用 oracle db,所以这可能会有所帮助

    package mypackage1;
    import java.text.*;
    import java.sql.*;
    import java.util.*;
    
    public class BlobTest 
    {
        public BlobTest()
        { }
    
        public static void main(String[] args)throws SQLException 
        {
           try {
               DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
               Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:rashmidb", "scott", "tiger");
               PreparedStatement ps = conn.prepareStatement("INSERT INTO BLOBTABLE VALUES (?)");
               oracle.sql.BLOB myBlob = oracle.sql.BLOB.createTemporary(conn, false, oracle.sql.BLOB.DURATION_SESSION);
               String myStr = "To test if the string gets inserted into the blob column";
               byte[] buff = myStr.getBytes();
               myBlob.putBytes(1,buff);
               ps.setBlob(1, myBlob);
               int count = ps.executeUpdate();
               ps.close();
               conn.close(); 
        }
         catch (Exception e) {
                          e.printStackTrace();
         }
       }
    
    }
    

    full article

    如果你使用休眠查看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-07
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多