将图片插入SQL SERVER2005数据库表中

 我来答
若绮彤ES
2010-06-25 · TA获得超过2928个赞
知道大有可为答主
回答量:4310
采纳率:100%
帮助的人:7313万
展开全部
如何将图片,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();
}

}
wuxin8882004
2010-06-26 · TA获得超过313个赞
知道小有建树答主
回答量:1074
采纳率:0%
帮助的人:330万
展开全部
那位高人太高了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式