1. 程式人生 > >如何將圖片插入資料庫

如何將圖片插入資料庫

         如何將圖片,Mp3 ,或是一些二進位制型別的資料插入到sqlserver,或是 Oracle 資料庫 . 方法是通過流進行操作.

建立一張測試表(sqlserver2000)

createtable[pictable] (
    
[id][int]identity (11notnull ,
    
[img][image]notnull 
on[primary] textimage_on [primary]
go


1,插入資料庫的方法(sqlserver2000)

this.getConnection() 為獲得連線的方法.

publicvoid insertPic(String path)
{
        Connection con 
=this.getConnection();
        String sql 
="insert into picTable values(?)" ;
        
try{
            PreparedStatement pstm 
= con.prepareStatement(sql);
            InputStream is 
=new FileInputStream(path);
            
            pstm.setBinaryStream(
1, is, is.available());
            
int count = pstm.executeUpdate();
            
if(count>0){
                System.out.println(
"插入成功");
            }
else{
                System.out.println(
"插入失敗");
            }

            is.close();
            pstm.close();
            con.close();
            
        }
catch (Exception e) 
{
            e.printStackTrace();
        }

    }

2,從資料庫中讀出來的方法.(sqlserver2000)

publicvoid readPic(int id){
        Connection con 
=this.getConnection();
        String sql 
="select  * from  picTable where id=?" ;
        
try{
            PreparedStatement pstm 
= con.prepareStatement(sql);
            pstm.setInt(
1, id);
            ResultSet rs 
= pstm.executeQuery();
            rs.next();
            InputStream is 
= rs.getBinaryStream(2);
            
            OutputStream os 
=new FileOutputStream("f:/temp.jpg");
            
byte[] buff =newbyte[1024];
            
int len = is.read(buff);
            
while( len !=-1 ){
                os.write(buff);
                len 
= is.read(buff);
            }

            System.out.println(
"寫入成功");
            is.close();
            os.close();
            pstm.close();
            con.close();
        }
catch (Exception e) {
            e.printStackTrace();
        }

    }

3,插入資料庫的方法(Oracle)

publicvoid insertBinary() {
        Connection con 
= MyConnection.getORACLEConnection();
        String sql 
="insert into testBinary values(?,?)";
        
try{
            con.setAutoCommit(
false);
            PreparedStatement pstm 
= con.prepareStatement(sql);
            pstm.setString(
1"a1");
            pstm.setBlob(
2, oracle.sql.BLOB.empty_lob());
            
int count = pstm.executeUpdate();
            pstm.close();
            pstm 
= con.prepareStatement("select  * from testBinary where id=?");
            pstm.setString(
1"a1");
            ResultSet rs 
= pstm.executeQuery();
            rs.next();
            oracle.sql.BLOB blob 
= (BLOB) rs.getBlob(2);
            OutputStream os 
= blob.getBinaryOutputStream();
            FileInputStream fi 
=new FileInputStream("E:/test.mp3");
            
byte[] buff =newbyte[1024];

            
int len = fi.read(buff);

            
while (len !=-1{
                os.write(buff);
                len 
= fi.read(buff);
            }

            pstm 
= con.prepareStatement(sql);
            pstm.setString(
1"a1");
            pstm.setBlob(
2, blob);
            
int res = pstm.executeUpdate();
            con.commit();
            pstm.close();
            con.close();

            
if (res >0{
                System.out.println(
"success");
            }

        }
catch (Exception ex) {
            ex.printStackTrace();
        }


    }

4,從資料庫中讀出來的方法.(Oracle)

publicvoid readerBinaryStream() {
        Connection con 
= MyConnection.getORACLEConnection();
        
try{
            java.sql.PreparedStatement pstm 
= con.prepareStatement(
                    
"select * from testBinary where id='a1'");
            ResultSet rs 
= pstm.executeQuery();
            rs.next();
            oracle.sql.BLOB blob 
= (BLOB) rs.getBlob(2);
            InputStream is 
= blob.getBinaryStream();
            FileOutputStream fi 
=new FileOutputStream("f:/aaaa.mp3");
            
byte[] buff =newbyte[1024];
            
int len = is.read(buff);
            
while (len !=-1{
                fi.write(buff);
                len 
= is.read(buff);

            }

            fi.close();

        }
catch (SQLException ex) {
        }
catch (FileNotFoundException ex) {
          ex.printStackTrace();
        }
catch (IOException ex) {
            ex.printStackTrace();
        }


    }