2012-11-23 32 views
-1

我目前正在開發一個需要圖像的應用程序,以便我嘗試在Oracle中使用blob和clob變量通過SQL查詢插入圖像,但是我無法通過此操作,而且我正在使用JAVA技術。如何在Oracle中添加圖像?

+0

類似的問題被問過....看看... http://stackoverflow.com/questions/8049974/oracle-database -blob -in-inputstream-in-java – AurA

+1

你試過了什麼?它只是谷歌搜索的問題來得到你想要的東西.. :) –

回答

0

下面是一個簡單的解決問題的方法:

FileInputStream fis; 
    try { 
     // Load JDBC driver "com.mysql.jdbc.Driver" 
     Class.forName("oracle.jdbc.driver.OracleDriver"); 

     /* Create a connection by using getConnection() method that takes 
     parameters of string type connection url, user name and password to 
     connect to database. */ 
     String connectionUrl = "your connection url"; 
     String userName = "connecting database username"; 
     String password = "corresponding password"; 
     connection = DriverManager.getConnection(connectionUrl ,userName , password); 
     // create a file object for image by specifying full path of image as parameter. 
     File image = new File("C:/image.jpg"); 
     /* prepareStatement() is used for create statement object that is 
      used for sending sql statements to the specified database. */ 
     psmnt = connection.prepareStatement("insert into save_image(name, city, image) values(?,?,?)"); 
     psmnt.setString(1,"name"); 
     psmnt.setString(2,"city"); 
     fis = new FileInputStream(image); 
     psmnt.setBinaryStream(3, (InputStream)fis, (int)(image.length())); 
     /* executeUpdate() method execute specified sql query. Here this query 
      insert data and image from specified address. */ 
     int s = psmnt.executeUpdate(); 
     if(s>0) { 
        System.out.println("Uploaded successfully !"); 
     } else { 
        System.out.println("unsucessfull to upload image."); 
     } 
    } catch (Exception e){ 
     //log useful information 
    } 
    finally{ 
     if (fis != null) { 
     fis.close(); 
     } 
     if(psmnt != null){ 
      psmnt.close(); 
     } 
     if (connection!= null && !connection.isClosed()) { 
      connection.close(); 
     } 
    }