SQL Server 对 Image字段进行操作
2012-01-02 15:58
447 查看
将图片写入数据库
public void testUploadPicture() {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://192.168.0.108:1433;databasename=music";
String user = "sa";
String password = "mmiku";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException ->" + e);
}
try {
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn
.prepareStatement("update webdb_prod_song set song_picture=? where song_id=12313");
// ps.setString(1, "123.jpg");
InputStream input = new FileInputStream("D:\\123.jpg");
ps.setBinaryStream(1, input, input.available());
ps.executeUpdate();
ps.close();
// 取出图片
ps = conn
.prepareStatement("select * from webdb_prod_song where song_id = ?");
ps.setString(1, "12313");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("song_picture");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("D:\\12.jpg");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
rs.close();
ps.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void testUploadPicture() {
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://192.168.0.108:1433;databasename=music";
String user = "sa";
String password = "mmiku";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException ->" + e);
}
try {
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn
.prepareStatement("update webdb_prod_song set song_picture=? where song_id=12313");
// ps.setString(1, "123.jpg");
InputStream input = new FileInputStream("D:\\123.jpg");
ps.setBinaryStream(1, input, input.available());
ps.executeUpdate();
ps.close();
// 取出图片
ps = conn
.prepareStatement("select * from webdb_prod_song where song_id = ?");
ps.setString(1, "12313");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("song_picture");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("D:\\12.jpg");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
}
out.close();
in.close();
rs.close();
ps.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
相关文章推荐
- SQL Server 对 Image字段进行操作
- C#操作Sql Server 2005的Image字段
- 在sql server 2005中操作image,varbinary字段
- SQL Server实现将特定字符串拆分并进行插入操作的方法
- 一次有趣的Debug——使用Lumigent Log Explorer对SQL Server事务日志进行分析,对SQL Server事务、操作进行撤销(恢复)
- SQL server 2008 对XML 字段的操作
- Robot---如何在脚本中对数据库(SQL SERVER)进行操作(一)
- 使用ImageMagick + Jmagick进行图片操作(续2)——实现高质量的图片处理
- 如何用运行access文件连接sql server中的表进行操作
- sql server 还原数据库时提示:数据库正在使用,无法进行操作的解决方法2
- PowerShell 连接 SQL SERVER 数据库进行操作
- SQL server 两个表中相同字段的操作
- SQL SERVER中对XML进行操作
- 如何SQL Server用事件探查器是只跟踪某个表某个字段的操作。
- sql server 还原数据库时提示数据库正在使用,无法进行操作的解决方法
- SQL Server中的XML数据进行insert、update、delete操作实现代码
- C# 对SQL Server中Image字段的读写
- SQL Server中的Image数据类型的操作
- SQL Server 2000升级sp4补丁的时候出现“以前进行的程序安装创建了挂起的文件操作”的错误!