如何将图片插入数据库

         如何将图片,Mp3 ,或是一些二进制类型的数据插入到sqlserver,或是 Oracle 数据库 . 方法是通过流进行操作.

创建一张测试表(sqlserver2000)

create   table   [ pictable ]  (
    
[ id ]   [ int ]   identity  ( 1 1 not   null  ,
    
[ img ]   [ image ]   not   null  
on   [ primary ]  textimage_on  [ primary ]
go


1,插入数据库的方法(sqlserver2000)

this.getConnection() 为获得连接的方法.

public   void  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)

public   void  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 = new byte[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)

public   void  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 = new byte[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)

public   void  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 = new byte[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();
        }


    }
图片插入数据库通常需要将图片转换为二进制数据,然后将其存储在BLOB类型的列。下面是一种在MySQL数据库图片插入的方法: 1. 创建一个表,并包含BLOB类型的列用于存储图片数据。例如: ``` CREATE TABLE myTable ( id INT(11) AUTO_INCREMENT PRIMARY KEY, image BLOB ); ``` 2. 读取图片文件并将其转换为二进制数据。假设图片文件路径为`/path/to/image.jpg`,可以使用以下代码将其转换为二进制数据: ``` import java.nio.file.Files; import java.nio.file.Paths; byte[] imageBytes = Files.readAllBytes(Paths.get("/path/to/image.jpg")); ``` 3. 将二进制数据插入数据库。可以使用JDBC库连接到数据库,并使用PreparedStatement对象插入数据。例如: ``` import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; // Connect to database String url = "jdbc:mysql://localhost/myDatabase"; String user = "myUsername"; String password = "myPassword"; Connection conn = DriverManager.getConnection(url, user, password); // Insert image data String sql = "INSERT INTO myTable (image) VALUES (?)"; PreparedStatement statement = conn.prepareStatement(sql); statement.setBytes(1, imageBytes); statement.executeUpdate(); // Close connection conn.close(); ``` 这将把图片数据插入到`myTable`表的`image`列。注意,这种方法并不是最优的,因为在数据库存储大量的二进制数据可能会影响性能。更好的方法是存储图片文件的路径,然后在需要显示图片时从文件系统读取它们。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值