JDBC实现文件、图片的存储实例
2016-03-17 10:53
513 查看
/*
实现的功能:
用数据库存储文本数据,并且读取出来放在当前项目里
分析:
难度不是很大,关键是掌握文件流,数据库的操作不是很多,但是文件流的读写比较多
日期:20131003
作者:烟大阳仔
*/
public class Ckb_test {
public static void main(String[] args) throws SQLException, IOException {
// TODO Auto-generated method stub
read();
}
static void create() throws SQLException, IOException
{
Connection conn=null;
PreparedStatement prest=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
String sql="insert into ckb_test(text) values(?)";
prest=conn.prepareStatement(sql);
File file=new File("src/cn/com/JDBC/JdbcUtils.java");
Reader reader=new BufferedReader(new FileReader(file));
prest.setCharacterStream(1, reader, (int)file.length());
//4.执行语句
int i=prest.executeUpdate();
reader.close();
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, prest, conn);
}
}
static void read() throws SQLException, IOException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
st=conn.createStatement();
//4.执行语句
resultset=st.executeQuery("select text from ckb_test");
//5.处理结果
while(resultset.next())
{
Clob clob=resultset.getClob(1);
Reader reader=clob.getCharacterStream();
//reader=resultset.getCharacterStream(1);
File file=new File("JdbcUtils.java");
Writer writer=new BufferedWriter(new FileWriter(file));
char[] buff=new char[1024];
for(int i=0;(i=reader.read(buff))>0;)
{
writer.write(buff,0,i);
}
writer.close();
reader.close();
}
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
}
----------------------------------------------------------------------------------------------------------
/*
实现的功能:
用数据库存储图片数据,并且读取出来放在当前项目里
分析:
难度不是很大,关键是掌握字节流,数据库的操作不是很多,但是文件流的读写比较多,注意更改图片的目录
日期:20131003
作者:烟大阳仔
*/
public class PictureBlob {
public static void main(String[] args) throws SQLException, IOException {
read();
}
static void create() throws SQLException, IOException
{
Connection conn=null;
PreparedStatement prest=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
String sql="insert into blob_test(big_bit) values(?)";
prest=conn.prepareStatement(sql);
File file=new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cxg.jpg");
InputStream in=new BufferedInputStream(new FileInputStream(file));
prest.setBinaryStream(1, in, (int)file.length());
//4.执行语句
int i=prest.executeUpdate();
in.close();
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, prest, conn);
}
}
static void read() throws SQLException, IOException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
st=conn.createStatement();
//4.执行语句
resultset=st.executeQuery("select big_bit from blob_test");
//5.处理结果
while(resultset.next())
{
Blob blob=resultset.getBlob(1);
InputStream in=blob.getBinaryStream();
//reader=resultset.getCharacterStream(1);
File file=new File("1.jpeg");
OutputStream out=new BufferedOutputStream(new FileOutputStream(file));
byte[] buff=new byte[1024];
for(int i=0;(i=in.read(buff))>0;)
{
out.write(buff,0,i);
}
out.close();
in.close();
}
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
}
实现的功能:
用数据库存储文本数据,并且读取出来放在当前项目里
分析:
难度不是很大,关键是掌握文件流,数据库的操作不是很多,但是文件流的读写比较多
日期:20131003
作者:烟大阳仔
*/
public class Ckb_test {
public static void main(String[] args) throws SQLException, IOException {
// TODO Auto-generated method stub
read();
}
static void create() throws SQLException, IOException
{
Connection conn=null;
PreparedStatement prest=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
String sql="insert into ckb_test(text) values(?)";
prest=conn.prepareStatement(sql);
File file=new File("src/cn/com/JDBC/JdbcUtils.java");
Reader reader=new BufferedReader(new FileReader(file));
prest.setCharacterStream(1, reader, (int)file.length());
//4.执行语句
int i=prest.executeUpdate();
reader.close();
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, prest, conn);
}
}
static void read() throws SQLException, IOException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
st=conn.createStatement();
//4.执行语句
resultset=st.executeQuery("select text from ckb_test");
//5.处理结果
while(resultset.next())
{
Clob clob=resultset.getClob(1);
Reader reader=clob.getCharacterStream();
//reader=resultset.getCharacterStream(1);
File file=new File("JdbcUtils.java");
Writer writer=new BufferedWriter(new FileWriter(file));
char[] buff=new char[1024];
for(int i=0;(i=reader.read(buff))>0;)
{
writer.write(buff,0,i);
}
writer.close();
reader.close();
}
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
}
----------------------------------------------------------------------------------------------------------
/*
实现的功能:
用数据库存储图片数据,并且读取出来放在当前项目里
分析:
难度不是很大,关键是掌握字节流,数据库的操作不是很多,但是文件流的读写比较多,注意更改图片的目录
日期:20131003
作者:烟大阳仔
*/
public class PictureBlob {
public static void main(String[] args) throws SQLException, IOException {
read();
}
static void create() throws SQLException, IOException
{
Connection conn=null;
PreparedStatement prest=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
String sql="insert into blob_test(big_bit) values(?)";
prest=conn.prepareStatement(sql);
File file=new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cxg.jpg");
InputStream in=new BufferedInputStream(new FileInputStream(file));
prest.setBinaryStream(1, in, (int)file.length());
//4.执行语句
int i=prest.executeUpdate();
in.close();
System.out.println("i="+i);
} finally
{
JdbcUtils.free(resultset, prest, conn);
}
}
static void read() throws SQLException, IOException
{
Connection conn=null;
Statement st=null;
ResultSet resultset=null;
try {
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
st=conn.createStatement();
//4.执行语句
resultset=st.executeQuery("select big_bit from blob_test");
//5.处理结果
while(resultset.next())
{
Blob blob=resultset.getBlob(1);
InputStream in=blob.getBinaryStream();
//reader=resultset.getCharacterStream(1);
File file=new File("1.jpeg");
OutputStream out=new BufferedOutputStream(new FileOutputStream(file));
byte[] buff=new byte[1024];
for(int i=0;(i=in.read(buff))>0;)
{
out.write(buff,0,i);
}
out.close();
in.close();
}
} finally
{
JdbcUtils.free(resultset, st, conn);
}
}
}
相关文章推荐
- 错误消息:在公司代码 & 中没有激活成本会计核算
- 网上移动收藏夹
- ccnet部署的项目子项
- 代理的使用
- 【Java80小白建站系列】4.网站备案
- Redis 学习 ---- 3.链表 Linked List
- 手把手教Windows下搭建git服务器
- android日常干货
- java 动态代理
- tomcat------https单向认证和双向认证
- JDBC数据库的API对照实例学习
- 《Linux内核设计与实现》课本第一章&第二章学习笔记
- linux grep打印匹配的上下几行
- jsp导出excel
- App关键页面埋点基础
- docker+node+Cordova 多平台流媒体直播系统 一 centos6.5 源码编译安装node5.8(2)
- 省份、城市列表排序规则
- SharePoint Server 2016 RTM 发布
- 我所认识的垃圾回收
- object-c 的runtime.h