PutFile.java
import java.io.File;
import java.io.FileInputStream;
import java.sql.*; public class PutFile { public static void main(String args[]) { Connection conn = null;
PreparedStatement psmd = null;
String sql = null; try {
conn = ConnUtil.getConn();
File file = new File("F:\\1.jpg");// 要转换的文件
FileInputStream inputStream = new FileInputStream(file);// 将文件按二进制存储在一个字段内
// CREATE TABLE images (imgname text, img bytea);
sql = " insert into images values(?,?)";
psmd = conn.prepareStatement(sql);
psmd.setString(1, file.getName());
psmd.setBinaryStream(2, inputStream, (int) file.length());
int rs = psmd.executeUpdate(); if (rs < 0) {
System.out.println("存入数据失败!!!");
} else {
System.out.println("存入数据成功!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
if (psmd != null) {
try {
psmd.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
GetFile.java
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.*; public class GetFile { public static void main(String args[]) { Connection conn = null;
PreparedStatement psmd = null;
String sql = null;
OutputStream out = null; try {
conn = ConnUtil.getConn();
// CREATE TABLE images (imgname text, img bytea);
sql = " SELECT img FROM images WHERE imgname = ?";
psmd = conn.prepareStatement(sql);
psmd.setString(1, "1.jpg");
ResultSet rs = psmd.executeQuery();
while(rs.next()) {
byte[] imgBytes = rs.getBytes(1);
// 实例化OutputStream对象,在f盘创建一个图片文件
out = new FileOutputStream("f:\\2.jpg");
// 将文件输出,内容则为byte数组里面的数据
out.write(imgBytes);
out.flush();
System.out.println("获取数据成功!!!");
}
} catch (Exception e) {
e.printStackTrace();
} if (psmd != null) {
try {
psmd.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
ConnUtil.java import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; public class ConnUtil { public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.scistor.swift.jdbc.Driver"); // 驱动
String url = "jdbc:swift://192.168.8.103:2345/swfit"; // 连接数据库的url
try {
conn = DriverManager.getConnection(url, "super", "111111"); // super 为数据库用户名,111111为密码
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} return conn;
} }

  

JDBC存取二进制文件示例的更多相关文章

  1. Spring JDBC常用方法详细示例

    Spring JDBC使用简单,代码简洁明了,非常适合快速开发的小型项目.下面对开发中常用的增删改查等方法逐一示例说明使用方法 1 环境准备 启动MySQL, 创建一个名为test的数据库 创建Mav ...

  2. Spring JDBC SqlUpdate类示例

    org.springframework.jdbc.object.SqlUpdate类提供了表示SQL更新的可重用操作对象. 使用到的 Student 表的结构如下 - CREATE TABLE Stu ...

  3. Spring JDBC StoredProcedure类示例

    org.springframework.jdbc.core.StoredProcedure类是RDBMS存储过程的对象抽象的超类.这个类是抽象的,目的是让子类将提供一个用于调用的类型化方法,该方法委托 ...

  4. Spring JDBC SqlQuery类示例

    org.springframework.jdbc.object.SqlQuery类提供了表示SQL查询的可重用操作对象. 使用到的 Student 表的结构如下 - CREATE TABLE Stud ...

  5. Spring JDBC SimpleJdbcCall类示例

    org.springframework.jdbc.core.SimpleJdbcCall类是表示对存储过程或存储函数的调用的多线程,可重用的对象. 它提供元数据处理以简化访问基本存储过程/函数所需的代 ...

  6. Spring JDBC SimpleJdbcInsert类示例

    org.springframework.jdbc.core.SimpleJdbcInsert类是一个多线程,可重用的对象,为将数据插入表提供了易用的功能.它提供元数据处理以简化构建基本insert语句 ...

  7. Spring JDBC RowMapper接口示例

    JdbcTemplate类使用org.springframework.jdbc.core.RowMapper <T>接口在每行的基础上映射ResultSet的行.该接口的实现执行将每行映射 ...

  8. Spring JDBC ResultSetExtractor接口示例

    org.springframework.jdbc.core.ResultSetExtractor接口是JdbcTemplate的查询方法使用的回调接口.此接口的实现执行从ResultSet提取结果的实 ...

  9. Spring JDBC PreparedStatementSetter接口示例

    org.springframework.jdbc.core.PreparedStatementSetter接口充当JdbcTemplate类使用的一般回调接口.该接口在JdbcTemplate类提供的 ...

随机推荐

  1. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  2. JNI的一些知识:

    JNI字段描述符"([Ljava/lang/String;)V" 2012-05-31 12:16:09| 分类: Android |举报|字号 订阅 "([Ljava/ ...

  3. node.js 对接公众平台

    http://www.tfan.org/wp-content/uploads/使用-Nodejs-和-MongoDB-开发高性能微信公众平台应用.pdf

  4. 第五章 CSS页面布局基础

    1.标准文档流 在正常流中,在没有使用浮动或者定位的情况下,文本元素按照从上到下.从左到右的格式布局.这是浏览器的默认行为.在正常流中,块级元素从上到下依次排列,而行级元素从左到右依次排列.正常流中的 ...

  5. 启动、关闭Service

    //获取程序界面中的start.stop两个按钮 start = (Button) findViewById(R.id.start); stop = (Button) findViewById(R.i ...

  6. apache日志轮转

    apache默认的日志配置为:     ErrorLog "logs/error_log"     CustomLog "logs/access_log" co ...

  7. Admob(6.12.x)符号未定义错误的解决方法(IOS)

    在升级Admob的SDK版本到6.12.x时, 按照官方文档操作(https://developers.google.com/mobile-ads-sdk/docs/#ios), 添加如下framew ...

  8. 二模 (13)day1

    第一题: 题目大意: N个发射站排成一排,求每个发射站左右第一个比它高的发射站. N<=1000000 解题过程: 1.前几天做poj的时候刚好在discuss里看到有一个神奇的东东叫单调栈,正 ...

  9. 使用AWT组件实现验证码功能

    import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D ...

  10. C语言输出规定长度的整数,不够位数前面补零

    今天在做ACM题目的时候,遇到了这么一个问题,还真别说,这个以前真的没用过,当时就傻掉了,还好这个世界有Google,通过搜索了解了输出这种格式的C语言实现方法.但是没有找到C++的实现方法,希望知道 ...