您的位置:首页 > 大数据

初学JDBC(六)-使用Clob与Blob处理大数据对象

2016-11-19 22:17 477 查看
       上一篇博客中我讲了使用ResultSet接口结果集对mysql数据库中表的内容进行查询操作,使用了泛型和List列表对数据库中的表的内容进行查询,这一方法需要读者着重掌握;上面的博客详细的讲了数据库中表的内容的增删改查操作,这一篇博客我们来说说使用JDBC处理大数据对象时候怎么去用Clob和Blob接口。

      Clob:SQL Clob是内置类型,它将字符大对象(Character Large Object) 存储为数据库表某一行中的一个列值。默认的情况下,驱动程序使用SQL locator(Clob)实现Clob对象,这意味着Clob对象包含一个指向SQL Clob数据的逻辑指针而不是数据本身。Clob对象在它被创建的事物处理期间有效。Clob接口提供一些方法来获取SQL Clob(Character Large Object)值长度、在客户端实现Clob值以及搜索Clob值中的子字符串或Clob对象。接口ResultSet、PreparedStatement、CallableStatement中的方法允许编程人员访问SQL
Clob值。此外,此接口还拥有更新Clob值的方法。在Clob中可以存储大字符数据对象,比如长篇小说。

       Blob:SQL Blob是内置类型,它将二进制大对象(Binary Large Object)存储为数据库表某一行的一个列值。默认情况下,驱动程序使用SQL locator(Blob)实现Blob,这意味着Blob对象包含一个指向SQL Blob数据的逻辑指针而不是数据本身。Blob对象在它被创建的事物处理期间才有效。接口ResultSet、PreparedStatement、CallableStatement中的方法允许编程人员访问SQL Blob值。Blob接口提供一些方法来获取SQL
Blob(Binary Large Object)值的长度,在客户端实现Blob值以及确定Blob值中某一字节样本的位置。此外,此接口还有更新Blob值的方法。如果JDBC驱动程序支持该数据类型,则必须完全实现Blob接口的所有方法。

      上面是对Blob和Clob的介绍下面我们通过一个例子来对它有一些更详细的认识吧。

       首先在原先以后的表中增加一个context字段。

       alter table t_employee add context longtext;

       alter table t_employee add pic longblob;(如果你是第一次看这篇博客,请到前面的博客去浏览并发现那张表的详细创建)

       package com.panli.dbutil;
/**
* 连接数据库
*/
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbUtil {
//数据库驱动名字
private static String jdbcName = "com.mysql.jdbc.Driver";
//数据库协议地址
private static String dbUrl = "jdbc:mysql://localhost:3306/db_user";
//数据库用户名
private static String dbUser = "root";
//数据库密码
private static String dbPassword = "123456";

/**
* 获取连接
* @return
* @throws Exception
*/
public static Connection getCon() throws Exception{
Class.forName(jdbcName);
Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
return conn;
}

/**
* 关闭连接
* @param stmt
* @param conn
* @throws Exception
*/
public static void close(Statement stmt,Connection conn) throws Exception{
if(stmt!=null){
stmt.close();
if(conn!=null){
conn.close();
}
}
}

/**
* 关闭连接
* @param cstmt
* @param conn
* @throws Exception
*/
public static void close(CallableStatement cstmt, Connection conn) throws Exception{
if(cstmt!=null){
cstmt.close();
if(conn!=null){
conn.close();
}
}
}

/**
* 关闭连接
* @param pstmt
* @param conn
* @throws SQLException
*/
public static void close(PreparedStatement pstmt, Connection conn) throws SQLException{
if(pstmt!=null){
pstmt.close();
if(conn!=null){
conn.close();
}
}
}

/**
* 重载关闭方法
* @param pstmt
* @param conn
* @throws Exception
*/
public void close(ResultSet rs,PreparedStatement pstmt, Connection conn) throws Exception{
if(rs!=null){
rs.close();
if(pstmt!=null){
pstmt.close();
if(conn!=null){
conn.close();
}

}
}

}
}

       package com.panli.model;

import java.io.File;

/**
* model包下的employee类,对每个字段进行建模
* @author Peter
*
*/
public class Employee {
private int id;
private String userName;
private double salary;
private String job;
private int jobTypeId;
private File context;
private File pic;

/**
* 带有6个参数的构造方法
* @param userName
* @param salary
* @param job
* @param jobTypeId
* @param context
* @param pic
*/
public Employee(String userName, double salary, String job, int jobTypeId,
File context, File pic) {
super();
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
this.context = context;
this.pic = pic;
}

/**
* 具有5个参数的构造方法
* @param userName
* @param salary
* @param job
* @param jobTypeId
* @param context
*/
public Employee(String userName, double salary, String job, int jobTypeId,
File context) {
super();
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
this.context = context;
}

/**
* 默认的构造方法
*/
public Employee() {
super();
// TODO Auto-generated constructor stub
}

/**
* 带一个参数的构造方法
* @param id
*/
public Employee(int id) {
super();
this.id = id;
}

/**
* 带4个参数的构造方法
* @param userName
* @param salary
* @param job
* @param jobTypeId
*/
public Employee(String userName, double salary, String job, int jobTypeId) {
super();
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
}

/**
* 带参数的构造方法
* @param id
* @param userName
* @param salary
* @param job
* @param jobTypeId
*/
public Employee(int id, String userName, double salary, String job,
int jobTypeId) {
super();
this.id = id;
this.userName = userName;
this.salary = salary;
this.job = job;
this.jobTypeId = jobTypeId;
}

/**
* 重写toString()方法
*/
@Override
public String toString() {
return "Employee:[id=" + id + ", userName=" + userName + ", salary="
+ salary + ", job=" + job + ", jobTypeId=" + jobTypeId + "]";
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getJobTypeId() {
return jobTypeId;
}
public void setJobTypeId(int jobTypeId) {
this.jobTypeId = jobTypeId;
}

public File getContext() {
return context;
}

public void setContext(File context) {
this.context = context;
}

public File getPic() {
return pic;
}

public void setPic(File pic) {
this.pic = pic;
}

}

       package com.panli.dao;
/**
* 对具有大数据对象的表内容进行增加和查询操作
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.panli.dbutil.DbUtil;
import com.panli.model.Employee;

public class ASCBEmployeeDao {
private static DbUtil dbUtil = new DbUtil();

/**
* 插入具有大数据对象blob和clob对象的内容
* @param employee
* @return
*/
public static int addData(Employee employee) throws Exception{
Connection conn = dbUtil.getCon();
String sql = "insert into t_employee values(null, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, employee.getUserName());
pstmt.setDouble(2, employee.getSalary());
pstmt.setString(3, employee.getJob());
pstmt.setInt(4, employee.getJobTypeId());
//添加具有clob的大数据对象
File context = employee.getContext();
InputStream inputStream = new FileInputStream(context);
pstmt.setAsciiStream(5, inputStream, context.length());
//添加具有blob的大数据对象
File pic = employee.getPic();
InputStream inputStream1 = new FileInputStream(pic);
pstmt.setBinaryStream(6, inputStream1, pic.length());
int result = pstmt.executeUpdate();
dbUtil.close(pstmt, conn);
return result;
}

/**
* 查询具有clob和blob的大数据对象的表
* @param employee
*/
public static void selectData(Employee employee)throws Exception{
Connection conn = dbUtil.getCon();
String sql = "select * from t_employee where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, employee.getId());
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
String userName = rs.getString("userName");
double salary = rs.getDouble("salary");
String job = rs.getString("job");
int jobTypeId = rs.getInt("jobTypeId");
Clob c = rs.getClob("context");
String context = c.getSubString(1, (int)c.length());
Blob b = rs.getBlob("pic");
FileOutputStream fos = new FileOutputStream("d:/test/lindan1.jpg");
fos.write(b.getBytes(1, (int)b.length()));
fos.close();
System.out.println("Employee:[userName=" + userName + ", salary="
+ salary + ", job=" + job + ", jobTypeId=" + jobTypeId + "]");
System.out.println("context:"+context);
}
}

}

       package com.panli.test;
/**
* 大数据对象的测试类
*/
import java.io.File;

import com.panli.dao.ASCBEmployeeDao;
import com.panli.model.Employee;

public class Test3 {
private static ASCBEmployeeDao ascbemployeeDao = new ASCBEmployeeDao();

public static void main(String[] args) throws Exception{
/**
* 对添加大数据对象的测试
*/
/*
File context = new File("D:/test/lindan.txt");
File pic = new File("D:/test/lindan.jpg");
Employee employee = new Employee("林丹", 800, "羽毛球", 1, context, pic);
int result = ascbemployeeDao.addData(employee);
if(result==1){
System.out.println("数据插入成功!");
}else{
System.out.println("数据插入失败!");
}
*/

//查询大数据对象
Employee employee = new Employee(11);
ascbemployeeDao.selectData(employee);
}
}

       上面就是有大数据对象用Clob和Blob怎样去处理,看懂了么?加油哦!文本文件和图片我就没有长传了,地址为:D盘test目录下lindan.txt和lindan.jpg文件。

      Ps注:如果遇到这样的错误提示:

        Exception in thread "main" java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setAsciiStream(ILjava/io/InputStream;J)V
at com.panli.dao.ASEmployeeDao.addData(ASEmployeeDao.java:31)
at com.panli.test.Test3.main(Test3.java:14)

       见到这样的错误提示为mysql的Jar包版本太低,更换一个高版本的Jar看一下。

        

       
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: