[學習筆記]java+oracle 存儲圖片到數據庫中 a---存儲

package cn.java;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * 
@author he
 
*/

public class SaveER {

   
   
public void save(String file){
        
{

            FileInputStream fis 
= null;
             File f
=new File(file);
                Connection conn 
= null;
                PreparedStatement pstmt 
= null;
            
try {
              
                String sql 
= "insert into ss values(15,?)";
                conn 
= DBUtil.getConnection();
                pstmt 
= conn.prepareStatement(sql);
                fis 
= new FileInputStream(f);
               
// pstmt.setBinaryStream(1, fis,fis.available());//保存普通數據
               pstmt.setBinaryStream(1, fis, (int)f.length());//保存圖片採用該句話

               pstmt.execute();
                System.out.println(
"保存成功!");
            }
 catch (IOException ex) {
                Logger.getLogger(SaveER.
class.getName()).log(Level.SEVERE, null, ex);
            }
 catch (SQLException ex) {
                Logger.getLogger(SaveER.
class.getName()).log(Level.SEVERE, null, ex);
            }


            
finally {
                
try {
                    fis.close();
                    DBUtil.close(
null, pstmt, conn);
                }
 catch (IOException ex) {
                    Logger.getLogger(SaveER.
class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        }

   
   }

   
public static void main(String args[]){
   SaveER er
=new SaveER();
   er.save(
"D:/1.jpg");
   }

    
}

 

 

 

用到的數據表爲

create table ss(

id int(4),

pic blob);

 

發佈了62 篇原創文章 · 獲贊 1 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章