谁有用java将图片java 批量保存数据库到数据库中然后取出来的源代码?

java操作保存图片到oracle数据库及获取图片到页面显示 - 敲代码 - ITeye博客
博客分类:
《--------保存图片到数据库--------------》public void savePic(InputStream is){
String insertsql = "insert into pic(id,pic) values(1,empty_blob())";
Connection conn =
Statement stmt =
PreparedStatement ps =
ResultSet rs =
conn = ds.getConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
int rt = stmt.executeUpdate(insertsql);
if(rt & 0){
System.out.println("initialization succeed!!");
String preCursor = "select id,pic from pic where id='1' for update";
rs = stmt.executeQuery(preCursor);
rs.next();
Blob b = (Blob)rs.getBlob("pic");
BufferedOutputStream bos = new BufferedOutputStream(b.setBinaryStream(0L));
BufferedInputStream bis = new BufferedInputStream(is);
while((bytes = bis.read()) != -1){
bos.write(bytes);
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}《--------获取图片到到页面--------------》public JPEGImageEncoder getPic(String id,HttpServletResponse res) throws IOException{
Connection conn =
Statement stmt =
PreparedStatement ps =
ResultSet rs =
BufferedInputStream imageStream =
BufferedImage image =
JPEGImageEncoder encoder =
ServletOutputStream sos =
res.getOutputStream();
conn = ds.getConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
System.out.println("initialization succeed!!");
String preCursor = "select id,pic from pic where id='1'";
rs = stmt.executeQuery(preCursor);
rs.next();
imageStream = new BufferedInputStream(((Blob)rs.getBlob(2)).getBinaryStream());
image = ImageIO.read(imageStream);
JPEGCodec.createJPEGEncoder(sos);
encoder.encode(image);
imageStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
从数据库获取图片,导出到本地的完整代码:
import java.awt.image.BufferedIimport java.io.BufferedInputSimport java.io.Fimport java.io.FileOutputSimport java.io.IOEimport java.io.OutputSimport java.sql.Bimport java.sql.Cimport java.sql.DriverMimport java.sql.PreparedSimport java.sql.ResultSimport java.sql.SQLEimport java.sql.Simport javax.imageio.ImageIO;import javax.servlet.ServletOutputSimport javax.servlet.http.HttpServletRimport com.sun.image.codec.jpeg.JPEGCimport com.sun.image.codec.jpeg.JPEGImageEpublic class DBManager {
private String url = "jdbc:oracle:thin:@10.23.65.21:1521:orcl";
private String userName = "test";
private String password = "test";
private String driver
= "oracle.jdbc.driver.OracleDriver";
public DBManager(){
Class.forName(driver);
conn = DriverManager.getConnection(url, userName, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
public void getPic(String picPath) throws IOException{
Statement stmt =
PreparedStatement ps =
ResultSet rs =
BufferedInputStream imageStream =
BufferedImage image =
JPEGImageEncoder encoder =
conn.setAutoCommit(false);
stmt = conn.createStatement();
System.out.println("initialization succeed!!");
String preCursor = "select id,zp from zpb where id='2220087'";
rs = stmt.executeQuery(preCursor);
while(rs.next()){
imageStream = new BufferedInputStream(((Blob)rs.getBlob(2)).getBinaryStream());
String id = rs.getString(1);
File picturePath = new File(picPath +"/"+ id +".jpg");
boolean created
if(!picturePath.exists()){
created = picturePath.createNewFile();
if(created){//
ServletOutputStream sos =
res.getOutputStream();
OutputStream ostream = new FileOutputStream(picturePath);
image = ImageIO.read(imageStream);
JPEGCodec.createJPEGEncoder(ostream);
encoder.encode(image);
imageStream.close();
ostream.flush();
ostream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
public static void main(String[] s){
DBManager manager = new DBManager();
String localPath = "E:/testpics";
manager.getPic(localPath);
} catch (IOException e) {
e.printStackTrace();
浏览: 151939 次
来自: 山西
wgrmmtmr 写道let g:miniBufExplMap ...
let g:miniBufExplMapWindowsNavV ...
avi9111 写道还是同样问题,你这个代码ctrl+c和ct ...
还是同样问题,
你这个代码
ctrl+c和ctrl+v粘贴能处 ...
ILoveDOUZHOU 写道windows下为什么使用vim ...二:获取数据库连接
1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar
2:写代码连接数据库,如下:
4 package com.hlcui.
6 import java.sql.C
7 import java.sql.DriverM
8 import java.sql.SQLE
* @author Administrator
14 public class DBUtil {
// 定义数据库连接参数
public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/test";
public static final String USERNAME = "root";
public static final String PASSWORD = "root";
// 注册数据库驱动
Class.forName(DRIVER_CLASS_NAME);
} catch (ClassNotFoundException e) {
System.out.println("注册失败!");
e.printStackTrace();
// 获取连接
public static Connection getConn() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 关闭连接
public static void closeConn(Connection conn) {
if (null != conn) {
conn.close();
} catch (SQLException e) {
System.out.println("关闭连接失败!");
e.printStackTrace();
public static void main(String[] args) throws SQLException {
System.out.println(DBUtil.getConn());
三:封装读取图片的流
4 package com.hlcui.
6 import java.io.F
7 import java.io.FileInputS
8 import java.io.FileOutputS
9 import java.io.IOE
10 import java.io.InputS
* @author Administrator
16 public class ImageUtil {
// 读取本地图片获取输入流
public static FileInputStream readImage(String path) throws IOException {
return new FileInputStream(new File(path));
// 读取表中图片获取输出流
public static void readBin2Image(InputStream in, String targetPath) {
File file = new File(targetPath);
String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
if (!file.exists()) {
new File(path).mkdir();
FileOutputStream fos = null;
fos = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) != -1) {
fos.write(buf, 0, len);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != fos) {
fos.close();
} catch (IOException e) {
e.printStackTrace();
四:实现图片(本地、数据库互相传输)
4 package com.hlcui.
6 import java.io.FileInputS
7 import java.io.InputS
8 import java.sql.C
9 import java.sql.PreparedS
10 import java.sql.ResultS
11 import java.sql.SQLE
* @author Administrator 测试写入数据库以及从数据库中读取
16 public class ImageDemo {
// 将图片插入数据库
public static void readImage2DB() {
String path = "D:/1.png";
Connection conn = null;
PreparedStatement ps = null;
FileInputStream in = null;
in = ImageUtil.readImage(path);
conn = DBUtil.getConn();
String sql = "insert into photo (id,name,photo)values(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, "Tom");
ps.setBinaryStream(3, in, in.available());
int count = ps.executeUpdate();
if (count & 0) {
System.out.println("插入成功!");
System.out.println("插入失败!");
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn);
if (null != ps) {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
// 读取数据库中图片
public static void readDB2Image() {
String targetPath = "D:/image/1.png";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
conn = DBUtil.getConn();
String sql = "select * from photo where id =?";
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
rs = ps.executeQuery();
while (rs.next()) {
InputStream in = rs.getBinaryStream("photo");
ImageUtil.readBin2Image(in, targetPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
DBUtil.closeConn(conn);
if (rs != null) {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
if (ps != null) {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
public static void main(String[] args) {
//readImage2DB();
readDB2Image();
以上代码均已经验证!
阅读(...) 评论()Java+MySql图片数据保存与读取的具体实例
字体:[ ] 类型:转载 时间:
之前一直没有做过涉及到图片存储的应用,最近要做的东东涉及到了这个点,就做了一个小的例子算是对图片存储的初试吧
1.创建表: 代码如下:drop tCREATE TABLE photo (&&& id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,&&& name VARCHAR(100) COMMENT '名称',&&& photo blob COMMENT '照片')ENGINE=InnoDBDEFAULT CHARSET=utf8COLLATE=utf8_general_ 图片在MySql中的数据存储格式为blob类型;Blob是一个可以存储二进制文件的容器。
2.编写图片流数据存取的工具类: 代码如下:import java.io.Fimport java.io.FileInputSimport java.io.FileNotFoundEimport java.io.FileOutputSimport java.io.IOEimport java.io.InputS
public class ImageUtil {&&& private static File file =
&&& /**&&&& * 从本地文件读取图像的二进制流&&&& * &&&& * @param infile&&&& * @return&&&& */&&& public static FileInputStream getImageByte(String infile) {&&&&&&& FileInputStream imageByte =&&&&&&& file = new File(infile);&&&&&&& try {&&&&&&&&&&& imageByte = new FileInputStream(file);&&&&&&& } catch (FileNotFoundException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& return imageB&&& }
&&& /**&&&& * 将图片流读出为图片&&&& * &&&& * @param inputStream&&&& * @param path&&&& */&&& public static void readBlob(InputStream inputStream, String path) {&&&&&&& try {&&&&&&&&&&& FileOutputStream fileOutputStream = new FileOutputStream(path);&&&&&&&&&&& byte[] buffer = new byte[1024];&&&&&&&&&&& int len = 0;&&&&&&&&&&& while ((len = inputStream.read(buffer)) != -1) {&&&&&&&&&&&&&&& fileOutputStream.write(buffer, 0, len);&&&&&&&&&&& }&&&&&&&&&&& inputStream.close();&&&&&&&&&&& fileOutputStream.close();&&&&&&& } catch (FileNotFoundException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } catch (IOException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& }
3.将本地文件保存到数据库
  需要添加MySql的数据库驱动--mysql-connector-java-5.1.24-bin.jar 代码如下:import java.io.IOEimport java.io.InputSimport java.sql.Cimport java.sql.DriverMimport java.sql.PreparedSimport java.sql.SQLE
public class ImageInsert {&&& public static void main(String[] args) {&&&&&&& try {&&&&&&&&&&& Class.forName("com.mysql.jdbc.Driver").newInstance();&&&&&&& } catch (InstantiationException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } catch (IllegalAccessException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } catch (ClassNotFoundException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& String user = "root";&&&&&&& String password = "root";&&&&&&& String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";&&&&&&& Connection connection =&&&&&&& try {&&&&&&&&&&& connection = DriverManager.getConnection(url, user, password);&&&&&&& } catch (SQLException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& PreparedStatement preparedStatement =&&&&&&& InputStream inputStream =&&&&&&& inputStream = ImageUtil.getImageByte("D:\\temp\\photo1.png");&&&&&&& try {&&&&&&&&&&& String sql = "insert into photo(id,name,photo) values(?,?,?)";&&&&&&&&&&& preparedStatement = connection.prepareStatement(sql);&&&&&&&&&&& preparedStatement.setInt(1, 1);&&&&&&&&&&& preparedStatement.setString(2, "朱莉");&&&&&&&&&&& preparedStatement.setBinaryStream(3, inputStream,&&&&&&&&&&&&&&&&&&& inputStream.available());&&&&&&&&&&& preparedStatement.execute();&&&&&&& } catch (SQLException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } catch (IOException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } finally {&&&&&&&&&&& try {&&&&&&&&&&&&&&& if (inputStream != null)&&&&&&&&&&&&&&&&&&& inputStream.close();&&&&&&&&&&& } catch (IOException e) {&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&& } finally {&&&&&&&&&&&&&&& try {&&&&&&&&&&&&&&&&&&& if (preparedStatement != null)&&&&&&&&&&&&&&&&&&&&&&& preparedStatement.close();&&&&&&&&&&&&&&& } catch (SQLException e) {&&&&&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&&&&&& } finally {&&&&&&&&&&&&&&&&&&& try {&&&&&&&&&&&&&&&&&&&&&&& connection.close();&&&&&&&&&&&&&&&&&&& } catch (SQLException e) {&&&&&&&&&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }
&&& }}4.从数据库中读取并生成图片 代码如下:import java.io.IOEimport java.io.InputSimport java.sql.Cimport java.sql.DriverMimport java.sql.ResultSimport java.sql.SQLEimport java.sql.S
public class ImageGet {&&& public static void main(String[] args) {&&&&&&& try {&&&&&&&&&&& Class.forName("com.mysql.jdbc.Driver").newInstance();&&&&&&& } catch (InstantiationException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } catch (IllegalAccessException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } catch (ClassNotFoundException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& String user = "root";&&&&&&& String password = "root";&&&&&&& String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";&&&&&&& Connection connection =&&&&&&& try {&&&&&&&&&&& connection = DriverManager.getConnection(url, user, password);&&&&&&& } catch (SQLException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& Statement statement =&&&&&&& ResultSet resultSet =&&&&&&& InputStream inputStream =&&&&&&& try {&&&&&&&&&&& statement = connection.createStatement();&&&&&&&&&&& String sql = "select p.photo from photo p where id = 1";&&&&&&&&&&& resultSet = statement.executeQuery(sql);&&&&&&&&&&& resultSet.next();&&&&&&&&&&& inputStream = resultSet.getBinaryStream("photo");&&&&&&&&&&& ImageUtil.readBlob(inputStream, "D:\\temp\\photo2.png");&&&&&&& } catch (SQLException e) {&&&&&&&&&&& e.printStackTrace();&&&&&&& } finally {&&&&&&&&&&& try {&&&&&&&&&&&&&&& if (inputStream != null)&&&&&&&&&&&&&&&&&&& inputStream.close();&&&&&&&&&&& } catch (IOException e) {&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&& } finally {&&&&&&&&&&&&&&& try {&&&&&&&&&&&&&&&&&&& if (resultSet != null)&&&&&&&&&&&&&&&&&&&&&&& resultSet.close();&&&&&&&&&&&&&&& } catch (SQLException e) {&&&&&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&&&&&& } finally {&&&&&&&&&&&&&&&&&&& if (statement != null)&&&&&&&&&&&&&&&&&&&&&&& if (statement != null)&&&&&&&&&&&&&&&&&&&&&&&&&&& try {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& statement.close();&&&&&&&&&&&&&&&&&&&&&&&&&&& } catch (SQLException e) {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&&&&&&&&&&&&&&&&&& } finally {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& if (connection != null)&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& try {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& connection.close();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& } catch (SQLException e) {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& e.printStackTrace();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }
&&& }}5.Over!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具}

我要回帖

更多关于 java 批量保存数据库 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信