如何将图片插入数据库
2008-04-05 10:19
176 查看
如何将图片,Mp3 ,或是一些二进制类型的数据插入到sqlserver,或是 Oracle 数据库 . 方法是通过流进行操作.
创建一张测试表(sqlserver2000)
create table [pictable] (
[id] [int] identity (1, 1) 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();
}
}
创建一张测试表(sqlserver2000)
create table [pictable] (
[id] [int] identity (1, 1) 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();
}
}
相关文章推荐
- 如何将图片插入数据库
- 如何向数据库中插入和读取图片2
- 如何往数据库里插入图片名的一点总结
- 如何显示数据库中以 OLE 对象方式存储的 JPEG 图片
- 将图片插入数据库并使用asp.net读取出来的正确方法
- 如何把图片插入到Excel里
- 如何将图片储存在数据库里
- 如何向数据库插入带有单引号(')的字符串?--在做数据插入是一定要考虑,否则有单引号系统就会报错
- 如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中
- SQL将本地图片文件插入到数据库
- 如何在Altium Designer 软件PCB编辑器里插入图片或者公司LOGO
- 如何往数据库(Oracle)里存储图片和文件
- Hibernate向数据库插入图片
- 如何给对话框插入背景图片 VC++ MFC
- [转帖]如何将一张图片写入数据库SQL,并能正确在C#网页中显示
- [SqlServer]如何向数据库插入带有单引号(')的字符串
- 如何解决程序中新插入ACCESS数据库表的记录无法及时在数据库中更新的问题。
- SQL数据库,如何把一张表从一个数据库中插入到另外一个数据库?
- 如何在ASP.Net 中把图片存入数据库
- devexpress datagrid 与imageEdit以及如何存图片到数据库 z