import java.sql.*; import java.io.*; import oracle.sql.*; public class WriteBlob { public static void main(String[] args) { 銆try { 銆銆DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 銆銆Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","fankai","fankai"); 銆銆conn.setAutoCommit(false); 銆銆BLOB blob = null; 銆銆PreparedStatement pstmt = conn.prepareStatement("insert into javatest(name,content) values(?,empty_blob())"); 銆銆pstmt.setString(1,"fankai"); 銆銆pstmt.executeUpdate(); 銆銆pstmt.close(); 銆銆pstmt = conn.prepareStatement("select content from javatest where name= ? for update"); 銆銆pstmt.setString(1,"fankai"); 銆銆ResultSet rset = pstmt.executeQuery(); 銆銆if (rset.next()) blob = (BLOB) rset.getBlob(1); 銆銆String fileName = "oraclejdbc.jar"; 銆銆File f = new File(fileName); 銆銆FileInputStream fin = new FileInputStream(f); 銆銆System.out.println("file size = " + fin.available()); 銆銆pstmt = conn.prepareStatement("update javatest set content=? where name=?"); 銆銆OutputStream out = blob.getBinaryOutputStream(); 銆銆int count = -1, total = 0; 銆銆byte[] data = new byte[(int)fin.available()]; 銆銆fin.read(data); 銆銆out.write(data); 銆銆/* 銆銆byte[] data = new byte[blob.getBufferSize()]; 鍙︿竴縐嶅疄鐜版柟娉?鑺傜渷鍐呭瓨 銆銆while ((count = fin.read(data)) != -1) { 銆銆銆total += count; 銆銆銆out.write(data, 0, count); 銆銆} 銆銆*/ 銆銆fin.close(); 銆銆out.close(); 銆銆pstmt.setBlob(1,blob); 銆銆pstmt.setString(2,"fankai"); 銆銆pstmt.executeUpdate(); 銆銆pstmt.close(); 銆銆conn.commit(); 銆銆conn.close(); 銆} catch (SQLException e) { 銆 銆System.err.println(e.getMessage()); 銆銆e.printStackTrace(); 銆} catch (IOException e) { 銆銆System.err.println(e.getMessage()); 銆} } } |
into javatest(name,content) values(?,empty_blob()); |
select content from javatest where name= ? for update; |
java.sql.Blob oracle.sql.BLOB |
package com.fankai; import java.sql.Blob; public class Cat { 銆private String id; 銆private String name; 銆private char sex; 銆private float weight; 銆private Blob image; 銆public Cat() { } 銆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 char getSex() { return sex; } 銆public void setSex(char sex) { this.sex = sex; } 銆public float getWeight() { return weight; } 銆public void setWeight(float weight) { this.weight = weight; } 銆public Blob getImage() { return image; } 銆public void setImage(Blob image) { this.image = image;} } |
錛?xml version="1.0"?錛?BR>錛?DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"錛?BR> 錛渉ibernate-mapping錛?BR>錛渃lass name="com.fankai.Cat" table="cat"錛?BR>錛?--jcs-cache usage="read-only"/--錛?BR>錛渋d name="id" unsaved-value="null"錛?BR>錛済enerator class="uuid.hex"/錛?BR>錛?id錛?BR>錛減roperty name="name" length="16" not-null="true"/錛?BR>錛減roperty name="sex" length="1" not-null="true"/錛?BR>錛減roperty name="weight" /錛?BR>錛減roperty name="image" /錛?BR>錛?class錛?BR>錛?hibernate-mapping錛?/TD> |
package com.fankai; import java.sql.Blob; import net.sf.hibernate.*; import oracle.sql.*; import java.io.*; public class TestCatHibernate { 銆public static void testBlob() { 銆銆Session s = null; 銆銆byte[] buffer = new byte[1]; 銆銆buffer[0] = 1; 銆銆try { 銆銆銆SessionFactory sf = HibernateSessionFactory.getSessionFactory(); 銆銆銆s = sf.openSession(); 銆銆銆Transaction tx = s.beginTransaction(); 銆銆銆Cat c = new Cat(); 銆銆銆c.setName("Robbin"); 銆銆銆c.setImage(Hibernate.createBlob(buffer)); 銆銆銆s.save(c); 銆銆銆s.flush(); 銆銆銆s.refresh(c, LockMode.UPGRADE); 銆銆銆BLOB blob = (BLOB) c.getImage(); 銆銆銆OutputStream out = blob.getBinaryOutputStream(); 銆銆銆String fileName = "oraclejdbc.jar"; 銆銆銆File f = new File(fileName); 銆銆銆FileInputStream fin = new FileInputStream(f); 銆銆銆int count = -1, total = 0; 銆銆銆byte[] data = new byte[(int)fin.available()]; 銆銆銆fin.read(data); 銆銆銆out.write(data); 銆銆銆fin.close(); 銆銆銆out.close(); 銆銆銆s.flush(); 銆銆銆tx.commit(); 銆銆} catch (Exception e) { 銆銆銆System.out.println(e.getMessage()); 銆銆} finally { 銆銆銆if (s != null) 銆銆銆try { 銆銆銆銆s.close(); 銆銆銆} catch (Exception e) {} 銆銆} 銆} } |
錛渉ost id='yourweb:8080' app-dir='e:\blog\' class-update-interval='2'錛?BR>錛渨eb-app id='/'錛?BR>錛渄irectory-servlet id='false'/錛? 錛?web-app錛?BR>錛渆rror-log id='log/web-error.log'/錛? 錛渉ost錛?/TD> |
錛?xml version="1.0" encoding="ISO-8859-1"?錛? 錛?DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"錛?BR> 錛渨eb-app錛?BR> 錛渟ervlet錛?BR>錛渟ervlet-name錛瀉ction錛?servlet-name錛?BR>錛渟ervlet-class錛瀘rg.apache.struts.action.ActionServlet錛?servlet-class錛?BR>錛渋nit-param錛?BR>錛減aram-name錛瀋onfig錛?param-name錛?BR>錛減aram-value錛?WEB-INF/struts-config.xml錛?param-value錛?BR>錛?init-param錛?BR>錛渋nit-param錛?BR>錛減aram-name錛瀌ebug錛?param-name錛?BR>錛減aram-value錛?錛?param-value錛?BR>錛?init-param錛?BR>錛渋nit-param錛?BR>錛減aram-name錛瀌etail錛?param-name錛?BR>錛減aram-value錛?錛?param-value錛?BR>錛?init-param錛?BR>錛渓oad-on-startup錛?錛?load-on-startup錛?BR>錛?servlet錛?BR> 錛渟ervlet-mapping錛?BR>錛渟ervlet-name錛瀉ction錛?servlet-name錛?BR>錛渦rl-pattern錛?.do錛?url-pattern錛?BR>錛?servlet-mapping錛?BR> 錛?-- The Usual Welcome File List --錛?BR>錛渨elcome-file-list錛?BR>錛渨elcome-file錛瀒ndex.jsp錛?welcome-file錛?BR>錛?welcome-file-list錛?BR> 錛?-- Struts Tag Library Descriptors --錛?BR>錛渢aglib錛?BR>錛渢aglib-uri錛?tags/struts-bean錛?taglib-uri錛?BR>錛渢aglib-location錛?WEB-INF/struts-bean.tld錛?taglib-location錛?BR>錛?taglib錛?BR> 錛渢aglib錛?BR>錛渢aglib-uri錛?tags/struts-html錛?taglib-uri錛?BR>錛渢aglib-location錛?WEB-INF/struts-html.tld錛?taglib-location錛?BR>錛?taglib錛?BR> 錛渢aglib錛?BR>錛渢aglib-uri錛?tags/struts-logic錛?taglib-uri錛?BR>錛渢aglib-location錛?WEB-INF/struts-logic.tld錛?taglib-location錛?BR>錛?taglib錛?BR> 錛渢aglib錛?BR>錛渢aglib-uri錛?tags/struts-nested錛?taglib-uri錛?BR>錛渢aglib-location錛?WEB-INF/struts-nested.tld錛?taglib-location錛?BR>錛?taglib錛?BR> 錛渢aglib錛?BR>錛渢aglib-uri錛?tags/struts-tiles錛?taglib-uri錛?BR>錛渢aglib-location錛?WEB-INF/struts-tiles.tld錛?taglib-location錛?BR>錛?taglib錛?BR> 錛?web-app錛?/TD> |
hibernate2.jar commons-beanutils.jar commons-collections.jar commons-dbcp.jar commons-lang.jar commons-logging.jar commons-pool.jar dom4j.jar cglib-asm.jar connector.jar |
錛?xml version='1.0' encoding='utf-8'?錛?BR>錛?DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"錛? 錛渉ibernate-configuration錛?BR>錛渟ession-factory錛?BR> 錛減roperty name="hibernate.connection.url"錛?BR>jdbc:mysql://localhost:3306/yourdatabase 錛?property錛?BR>錛減roperty name="hibernate.connection.driver_class"錛?BR>org.gjt.mm.mysql.Driver 錛?property錛?BR>錛減roperty name="hibernate.connection.username"錛?BR>yourname 錛?property錛?BR>錛減roperty name="hibernate.connection.password"錛?BR>youyrpassword 錛?property錛?BR> 錛??- 鏄惁灝嗚繍琛屾湡鐢熸垚鐨凷QL杈撳嚭鍒版棩蹇椾互渚涜皟璇?-錛?BR>錛減roperty name="show_sql"錛瀎alse錛?property錛?BR> 錛?--dialect 錛屾瘡涓暟鎹簱閮芥湁鍏跺搴旂殑Dialet浠ュ尮閰嶅叾騫沖彴鐗規(guī)?-錛?BR>錛減ropertyname="dialect"錛瀗et.sf.hibernate.dialect.MySQLDialect錛?property錛?BR>錛??- 浜嬪姟綆$悊綾誨瀷錛岃繖閲屾垜浠嬌鐢↗DBC Transaction --錛?BR>錛減roperty name="transaction.factory_class"錛?BR>net.sf.hibernate.transaction.JDBCTransactionFactory錛?property錛?BR> 錛減roperty name="jdbc.batch_size"錛?0錛?property錛?BR>錛減roperty name="jdbc.use_scrollable_resultset"錛瀟rue錛?property錛?BR> 錛??鏄犲皠鏂囦歡閰嶇疆錛屾敞鎰忛厤緗枃浠跺悕蹇呴』鍖呭惈鍏剁浉瀵逛簬鏍圭殑鍏ㄨ礬寰?-錛?BR>錛渕apping resource="net/seerlog/hbm/Cat.hbm.xml"/錛?BR> 錛?session-factory錛?BR> 錛?hibernate-configuration錛?/TD> |
錛?DOCTYPE project [ 錛?ENTITY database SYSTEM "file:./config/database/hsqldb.xml"錛?BR>]錛?/TD> |
錛減roperty name="name" value="airline"/錛? |
錛減roperty name="build.gen-src.dir" value="${build.dir}/gen-src"/錛? |
錛渉ibernate destination="${build.gen-src.dir}" package="${name}.hibernate" genXDocletTags="false" genIntergratedCompositeKeys="false" javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper" /錛? |
|
|