CREATE TABLE Mytable_yq (Document varbinary (max), Yq varchar (20))
--select @xmlFileName = ' C:\TestXml.xml '
INSERT into myTable (Document)
SELECT * FROM
OPENROWSET (BULK N ' E:\20110330110932_61311.jpg ', Single_blob) as XMLDATA
INSERT into myTable (Document)
SELECT * FROM
OPENROWSET (BULK N ' D:\newviewhigh\serializable.txt ', Single_blob) as XMLDATA
SELECT * FROM MyTable
SELECT * FROM Mytable_yq
Import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.inputstream;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.statement;import java.util.ArrayList; Import Java.util.list;import Com.viewhigh.base.databaseconnection.entity.databaseconnection;import Com.viewhigh.base.databaseconnection.service.idatabaseconnectionservice;import Com.viewhigh.common.util.dbhelper;import Com.viewhigh.base.databaseconnection.service.impl.databaseconnectionserviceimpl;public class InsertBlobTest { public void INSERT2ZWDT (Connection conn) throws exception{//idatabaseconnectionservice Databaseconnectionservice = new Databaseconnectionserviceimpl ();//databaseconnection targetdatabaseconnection = databaseconnectionservice//. Getdatabaseconnectionbyid ("40288a5a57d13f0b0157d140652e0003 ");//connection conn = new DBHelper (targetdatabaseconnection)//objectoutputstream oos = new Objecto Utputstream (new FileOutputStream (file)); Oos.writeobject (user); Bytearrayoutputstream BOS = new Bytearrayoutputstream (); ObjectInputStream ins = new ObjectInputStream (oos); byte[] bytes = Bos.tobytearray (); InputStream bis = new Bytearrayinputstream (bytes); . getconnection (); Statement stmt = Conn.createstatement (); PreparedStatement pstmt= Null;conn.setautocommit (FALSE);//Cancel autocommit//================================/*Con Nection conn = getconnection (); Conn.setautocommit (FALSE); Set manual Submit PreparedStatement pstmt=null; */File File=new file ("D:\\newviewhigh\\serializable.txt"); InputStream in = new FileInputStream (file); System.out.println ("Test---------------->"); String sql= "INSERT into Mytable_yq (DOCUMENT,YQ)" + "VALUES (?,?)"; Pstmt=conn.preparestatement (SQL); Pstmt.setbinarystream (1, BIS, file.length ()); Pstmt.setstring (2, "yangqing"); Pstmt.execute (); Conn.commit (); Pstmt.close (); } public static void Main (string[] args) {String drivername= "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dburl= "Jdbc:sqlserver://localhost:1438;databasename=ah_datai_yd"; String username= "SA"; String userpwd= "SA"; try {class.forname (drivername); Connection dbconn=drivermanager.getconnection (DBURL,USERNAME,USERPWD); SYSTEM.OUT.PRINTLN ("Connection to Database Success"); Insertblobtest IB = new insertblobtest (); try {IB.INSERT2ZWDT (dbconn);} catch (Exception e) {//TODO auto-generated catch B Locke.printstacktrace ();} } catch (Exception e) {e.printstacktrace (); System.out.print ("Connection failed"); } }}
Import Java.io.file;import java.io.fileinputstream;import Java.io.inputstream;import Java.sql.Connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.statement;import Com.viewhigh.base.databaseconnection.entity.databaseconnection;import Com.viewhigh.base.databaseconnection.service.idatabaseconnectionservice;import Com.viewhigh.common.util.dbhelper;import Com.viewhigh.base.databaseconnection.service.impl.databaseconnectionserviceimpl;public class InsertBlob {//public void Insert2zwdt (String xmlfilename, Long index) throws exception{public void INSERT2ZWDT (Connection conn) throws Exc eption{//idatabaseconnectionservice databaseconnectionservice = new Databaseconnectionserviceimpl ();// DatabaseConnection targetdatabaseconnection = Databaseconnectionservice//.getdatabaseconnectionbyid (" 40288a5a57d13f0b0157d140652e0003 ");//connection conn = new DBHelper (targetdatabaseconnection)//.getConnection (); Statement stmt = Conn.createstatement (); Preparedstatement pstmt= Null;conn.setautocommit (FALSE);//Cancel autocommit//================================/*connection Conn = Getconnection (); Conn.setautocommit (FALSE); Set manual Submit PreparedStatement pstmt=null; */File File=new file ("D:\\newviewhigh\\serializable.txt"); InputStream in = new FileInputStream (file); System.out.println ("Test---------------->"); String sql= "INSERT into Mytable_yq (DOCUMENT,YQ)" + "VALUES (?,?)"; Pstmt=conn.preparestatement (SQL); Pstmt.setbinarystream (1, in, File.length ()); Pstmt.setstring (2, "yangqing"); /* Pstmt.setint (1, Index.intvalue ()); Pstmt.setstring (2, Exdata.getstsource ()); Pstmt.setstring (3, Exdata.getstkey ()); Pstmt.setstring (4, Exdata.getstversion ()); Pstmt.setstring (5, Exdata.getstvalue ()); Pstmt.setstring (6, Exdata.getstop ()); Pstmt.setstring (7, Exdata.getstmemo ()); Pstmt.Setbinarystream (8, in, File.length ()); Pstmt.setstring (9, Exdata.getstdest ()); */Pstmt.execute (); Conn.commit (); Pstmt.close (); }/*public void Insert2own (Exdata exdata, String xmlfilename, Long index) throws exception{file File=new file (x Mlfilename); InputStream in = new FileInputStream (file); datasending datasending = new datasending (); Datasending.setnmseqid (Index.intvalue ()); Datasending.setstsource (Exdata.getstsource ()); Datasending.setstkey (Exdata.getstkey ()); Datasending.setstversion (Exdata.getstversion ()); Datasending.setstvalue (Exdata.getstvalue ()); Datasending.setstop (Exdata.getstop ()); Datasending.setstmemo (Exdata.getstmemo ()); Datasending.setsenddate (New Date ()); Byte[] Data=new byte[]{}; Data=inputstreamtobyte (in); Datasending.setbldata (data); Datasending.setstdest (exdata.getstDest ()); Datasendingdao.save (datasending); } *//*private byte[] Inputstreamtobyte (InputStream in) throws ioexception{Bytearrayoutputstream baos=new ByteAr Rayoutputstream (); int ch; while ((Ch=in.read ())!=-1) {baos.write (CH); } byte[] Data=baos.tobytearray (); Baos.close (); return data; } */public static void Main (string[] args) {/*insertblob IB = new Insertblob (); try {IB.INSERT2ZWDT ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();} */string drivername= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dburl= "Jdbc:sqlserver://localhost:1438;databasename=ah_datai_yd"; String username= "SA"; String userpwd= "SA"; try {class.forname (drivername); Connection dbconn=drivermanager.getconnection (DBURL,USERNAME,USERPWD); SYSTEM.OUT.PRINTLN ("Connection to Database Success"); Insertblob IB = new Insertblob (); try {IB.INSERT2ZWDT (dbconn);} catch (Exception e) {// TODO auto-generated catch Blocke.printstacktrace ();} } catch (Exception e) {e.printstacktrace (); System.out.print ("Connection failed"); } }}
SQL Server binary Storage