帮忙看看java文件错在那了?谢谢!
lengren
|
1#
lengren 发表于 2002-08-13 17:28
帮忙看看java文件错在那了?谢谢!
我可以成功运行,但输出文件为0字节。这是为何?
建表语句: CREATE TABLE "H"."TESTFILE" ("ID" NUMBER(10) NOT NULL, "BLOBFILE" BLOB NOT NULL, PRIMARY KEY("ID")) TABLESPACE "H" //输入文件 package gonzuo; import java.sql.*; import oracle.jdbc.driver.* ; public class Dbtest { private String strConnection = "jdbcracle:thinhxy:1521:test" ; private String strOracleDriver = "oracle.jdbc.driver.OracleDriver" ; //private String strConnection = "jdbcdbc:tt" ; //private String strOracleDriver = "sun.jdbc.odbc.JdbcOdbcDriver" ; private Connection Conn= null ; private PreparedStatement state= null ; public boolean OpenConnection() { try { Class.forName(strOracleDriver); } catch(Exception e) { System.out.println("Load oracle driver Error!"+e); return false ; } System.out.println("Load jdbc Driver OK!"); if (Conn == null) { try { Conn = DriverManager.getConnection(strConnection,"h","h"); Conn.setAutoCommit(false); } catch(Exception e) { System.out.println("Open Connection Error!"+e); return false ; } } System.out.println("OpenConnection OK!"); return true ; } public boolean UpdateToDB() { String strXML = "" ; String sql = "INSERT INTO h.testfile (id,blobfile) valueS (?,?)"; String strFileName = "d:\\my.txt"; java.io.File afile = null ; try { state = Conn.prepareStatement(sql); state.setString(1,"3"); afile = new java.io.File(strFileName); System.out.println("File size is:["+afile.length()+"] byties!"); java.io.InputStream is = new java.io.FileInputStream(strFileName); state.setBinaryStream(2,is,(int)afile.length()); state.execute() ; } catch(Exception e) { System.out.println("Update Error!"+e); return false ; } System.out.println("Update OK!"); return true ; } public static void main(String argv[]) { Dbtest db = new Dbtest(); db.OpenConnection() ; db.UpdateToDB(); } } //输出文件 package gonzuo; import java.sql.*; import java.io.*; import oracle.sql.*; import oracle.jdbc.driver.*; public class Dbtestget { ResultSet rs=null; String username; String password; String url; Connection conn=null; public Dbtestget() { } //classname:数据库驱动程序类名 public Connection initializeDB(String classname) { try { Class.forName(classname); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); } try { conn = DriverManager.getConnection(url, username, password); } catch (Exception exInit) { System.err.println("initializeDB error:"+exInit); } return conn; } /** Connection con:数据源连接 String pathname:临时文件名 String sql:从数据库中查询存储的图片的SQL String Blobfield:数据库表存储图片的字段名 **/ public boolean getImage(Connection con,String pathname,String sql,String Blobfield) { try { FileOutputStream fou = new FileOutputStream(pathname); PreparedStatement ps = con.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { Blob imagedata = rs.getBlob(Blobfield); long size = imagedata.length(); System.out.println(size); byte[] idata =imagedata.getBytes(1,(int)size); System.out.println(idata.length); fou.write(idata); idata = null; } rs.close(); ps.close(); fou.close(); } catch (Exception ex ) { return false; } return true; } public static void main(String[] args) { Connection conn; Dbtestget getDBIMG1 = new Dbtestget(); getDBIMG1.username="h"; getDBIMG1.password ="h"; //String classname="oracle.jdbc.driver.OracleDriver"; //getDBIMG1.url = "jdbcracle:thinhxy:1521:tt"; String classname="oracle.jdbc.driver.OracleDriver"; getDBIMG1.url = "jdbcracle:thinhxy:1521:test"; conn=getDBIMG1.initializeDB(classname); //初始化Connection对象 String sql = "SELECT blobfile FROM H.testfile WHERE id = '3'";//查询到更新图形的SQL if (getDBIMG1.getImage(conn,"d:\\Dbtest.txt",sql,"blobfile")) System.out.println("Succeed in outputing") ; else System.out.println("failure in outputing") ; } } |