1

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

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

提前致谢 !

4

1 回答 1

2

I find this example. I see you use oracle db so this may help

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

if you use hibernate look at this

于 2013-02-11T11:14:06.280 回答