SQL server 用insert into 语句将图片插入到数据库表中

如何运用SQL查询分析器 运用insert into语句将图片插入到数据表的相应列中

假如 桌面图片为:1.jpg 。表为:photo,仅一列列名为:图片资料

insert into photo(图片资料)
values('?')
列数据类型 为image

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

创建一张测试表(sqlserver2000)

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

插入数据库的方法(sqlserver2000)

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

public void insertPic(String path)...{
Connection con = this.getConnection();
String sql = "insert into photo values(?)" ;
try ...{
PreparedStatement pstm = con.prepareStatement(sql);
InputStream is = new FileInputStream("C:\My Doc....\1.jpg");

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();
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-12-19
有难度的问题,之前我也想问这个问题。
貌似直接SQL不行,需要程序语言支持。
或者,你做一条数据,然后导出SQL,看能导出啥样子。
相似回答