Ibatis: the database uses DB2
1. Save local files to the database,
2. Read the data and put it in different directories.
Package COM. jinzy; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. reader; import Java. SQL. sqlexception; import Org. springframework. util. filecopyutils; import COM. ibatis. common. resources. resources; import COM. ibatis. sqlmap. client. s Qlmapclient; import COM. ibatis. sqlmap. client. sqlmapclientbuilder; public class blobclasstest {static sqlmapclient sqlmapper = NULL;/*** @ Param ARGs * @ throws sqlexception * @ throws ioexception */public static void main (string [] ARGs) throws exception {// todo auto-generated method stubreader reader = resources. getresourceasreader ("com/jinzy/maps/sqlmapconfigdb2.xml"); sqlmapper = sqlmapclientbuilder. Buildsqlmapclient (Reader); reader. close (); insertentity (); blobclass blobout = (blobclass) sqlmapper. queryforobject ("selectblobbyid", "idtxt4"); byte [] ins = blobout. getFile (); file filedir = getfilefrombytes (INS, "d :\\/4.txt");} // 2. public static file getfilefrombytes (byte [] B, string outputfile) {bufferedoutputstream stream = NULL; file = NULL; try {file = new file (outputfile); fileou Tputstream fstream = new fileoutputstream (File); stream = new bufferedoutputstream (fstream); stream. write (B);} catch (exception e) {e. printstacktrace ();} finally {If (stream! = NULL) {try {stream. close () ;}catch (ioexception E1) {e1.printstacktrace () ;}} return file ;}// 1. save the local file to the database public static void insertentity () throws ioexception, sqlexception {blobclass blob = new blobclass (); blob. setid ("idtxt4"); blob. setname ("name4"); file = new file ("C: \/4.txt"); inputstream in = new bufferedinputstream (New fileinputstream (File )); byte [] B = filecopyutils. copytobytearray (in); blob. setfile (B); sqlmapper. insert ("insert", blob );}}
package com.jinzy;public class BlobClass {private String id = null;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public byte[] getFile() {return file;}public void setFile(byte[] file) {this.file = file;}private String name = null;private byte[] file = null;}
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.ibm.db2.jcc.DB2Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:db2://localhost:50000/SampleDB2"/> <property name="JDBC.Username" value="db2admin"/> <property name="JDBC.Password" value="db2admin"/> </dataSource> </transactionManager> <sqlMap resource="com/jinzy/maps/User.xml"/> </sqlMapConfig>
<typeAlias alias="blob" type="com.jinzy.BlobClass"/> <select id="selectBlobByID" parameterClass= "String" resultClass="blob"> select * from "jzy"."blobTest" where id = #id# </select> <insert id="insert" parameterClass="blob"> insert into "jzy"."blobTest"(id, name, file) values (#id#,#name#,#file#)</insert>