第3.34课 上课 JDBC深入介绍, Java7结果集新特性

3_34

JDBC深入介绍

 数据库大对象存储

大数据也称之为LOB(Large Objects),LOB又分为:clob和blob,clob用于存储大文本,blob用于存储二进制数据,例如图像、声音、二进制文等。

 

MySQL使用Blob类型列存储二进制数据;

MySQL使用Text类型列存储大文本;

 

 

在实际开发中,有时是需要用程序将大文本或二进制数据直接保存到数据库中进行储存的。

 

例如:商品图片,用户上传的头像等等一些图片或者大文本数据要存储到数据库。

目前业界存储图片有两种做法:

 

1,把图片直接以二进制形式存储在数据库中

一般数据库提供一个二进制字段来存储二进制数据。

比如mysql中有个blob字段。

 

2,图片存储在磁盘上,数据库字段中保存的是图片的路径。(推荐)

 

 

 

---------------------存储和获取大文本数据-----------------------------------

通过使用PreparedStatement的setCharacterStream实现大文本存储

PreparedStatement.setCharacterStream(index, Reader, length);

 

通过使用ResultSet的getCharacterStream()或者getString()实现大文本获取

 

ResultSet. getCharacterStream(String columnLabel);

ResultSet.getString(String columnLabel);

 

 

---------------------存储和获取二进制数据-----------------------------------

通过使用PreparedStatement的setBinaryStream实现二进制数据存储

PreparedStatement. setBinaryStream(i, inputStream, length);

 

通过使用ResultSet的getBinaryStream()或者getBlob()实现二进制数据获取

 

ResultSet.getBinaryStream(String columnLabel);

ResultSet.getBlob(String columnLabel).getBinaryStream();

 

 

 

 

 

  建表语句

CREATE TABLE e_user(

id int(11) NOT NULL PRIMARY KEY auto_increment,

t_name varchar(10) default NULL,

t_content Text,

t_image Blob

);

  使用JDBC处理大文本

package com.yayadou.text;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Reader;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import com.mchange.v2.c3p0.ComboPooledDataSource;

 

/**

* 通过JDBC实现大文本数据存储到表中和从表中获取操作

*

*/

public class TextDemo {

static ComboPooledDataSource pool = null;

static {

pool = new ComboPooledDataSource();

}

public static void main(String[] args)throws Exception {

 

//1,获取连接

Connection conn = pool.getConnection();

//先执行saveText()方法,然后注释掉再执行getText()方法

//saveText(conn);

 

getText(conn);

}

 

/**

* 获取数据库表中的大文本数据

* @param conn

* @throws SQLException

* @throws IOException

*/

private static void getText(Connection conn) throws SQLException,

IOException {

PreparedStatement ps = conn.prepareStatement("select t_content from e_user where id = ?");

ps.setInt(1, 1);

ResultSet rs = ps.executeQuery();

if(rs.next()){

//获取大文本数据,返回一个字符输入流

Reader reader = rs.getCharacterStream("t_content");

//创建一个字符输出流,进行文件数据输出操作

FileWriter fw = new FileWriter("c:/my_about.txt");

char[] buff = new char[1024*2];

int len = -1;

while((len = reader.read(buff)) != -1){

fw.write(buff, 0, len);

}

fw.close();

reader.close();

ps.close();

conn.close();

}

}

 

 

/**

* 存储大文本

* @param conn

* @throws SQLException

* @throws FileNotFoundException

*/

private static void saveText(Connection conn) throws SQLException,

FileNotFoundException {

//2,获取执行对象

PreparedStatement ps = conn.prepareStatement("insert into e_user(t_name,t_content) values(?,?)");

//3,绑定需要存储的大文本数据

ps.setString(1, "tom");

//获取需要存储的文本文件路径

String path = TextDemo.class.getClassLoader().getResource("about.txt").getPath();

//创建文件对象

File file = new File(path);

//使用FileReader字符输入流绑定需要读取的文件

FileReader fr = new FileReader(file);

//设置参数

//参数1 ?号的占位符

//参数2 输入流

//参数3 文件大小

ps.setCharacterStream(2,fr,file.length());

//4,执行存储

int count = ps.executeUpdate();

String msg = count>0?"成功":"失败";

System.out.println("执行:"+msg);

//5,关闭连接

ps.close();

conn.close();

}

 

}

 

  使用JDBC处理二进制数据

package com.yayadou.text;

 

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import com.mchange.v2.c3p0.ComboPooledDataSource;

 

/**

* 通过JDBC实现二进制数据存储到表中和从表中获取操作

*

*/

public class ImageDemo {

static ComboPooledDataSource pool = null;

static {

pool = new ComboPooledDataSource();

}

public static void main(String[] args)throws Exception {

 

//1,获取连接

Connection conn = pool.getConnection();

//先执行saveImage()方法,然后注释掉再执行getImage()方法

//saveImage(conn);

 

getImage(conn);

}

 

/**

* 获取数据库表中的二进制数据

* @param conn

* @throws SQLException

* @throws IOException

*/

private static void getImage(Connection conn) throws SQLException,

IOException {

PreparedStatement ps = conn.prepareStatement("select t_image from e_user where id = ?");

ps.setInt(1, 3);

ResultSet rs = ps.executeQuery();

if(rs.next()){

//获取大文本数据,返回一个字符输入流

InputStream in = rs.getBinaryStream("t_image");

//创建一个字符输出流,进行文件数据输出操作

FileOutputStream fos = new FileOutputStream("c:/my_yayadou.png");

byte[] buff = new byte[1024*2];

int len = 0;

while ((len = in.read(buff)) > 0) {

fos.write(buff, 0, len);

}

ps.close();

conn.close();

}

}

 

/**

* 存储二进制数据

* @param conn

* @throws SQLException

* @throws FileNotFoundException

*/

private static void saveImage(Connection conn) throws SQLException,

FileNotFoundException {

//2,获取执行对象

PreparedStatement ps = conn.prepareStatement("insert into e_user(t_name,t_image) values(?,?)");

//3,绑定需要存储的数据

ps.setString(1, "tom");

//获取需要存储的二进制数据输入流

InputStream in = ImageDemo.class.getClassLoader().getResourceAsStream("yayadou-head.jpg");

//设置参数

//参数1 ?号的占位符

//参数2 输入流

ps.setBinaryStream(2, in);

 

//4,执行存储

int count = ps.executeUpdate();

String msg = count>0?"成功":"失败";

System.out.println("执行:"+msg);

//5,关闭连接

ps.close();

conn.close();

}

 

}

posted on 2018-01-31 22:59  東風★破  阅读(186)  评论(0)    收藏  举报

导航