package com.ibm.db;
import java.io.*;
import java.sql.*;
public class TestMysql {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// text
//insertBlob("C:\\D\\sql_oracle_temp.xml");
//queryBlob("C:\\D\\sql_oracle_temp.xml.txt");
//image
//insertBlob("C:\\D\\007.mp3");
//queryBlob("C:\\D\\008.mp3");
/**
* 在处理blob字段时候,由于直接处理的是二进制流,所以没啥问题。
* 在处理clob字段的时候,由于数据库对clob是以字符的形式进行存储,
* 这就有一个编码问题。本文虽然成功的插入读取了clob字段,但是还没有解决乱码问题,
* 因为JDBC在获取到clob的时候,已经对其进行了编码,Reader reader = rs.getCharacterStream(1);
* 这就导致了编码的混乱,如果要彻底解决,还需要看看MySQL驱动的实现。通过非常规手段来解决。为了绕开此问题,
* 可以将clob的数据存储为blog来操作,可以避免此问题。
*/
try {
insertClob("C:\\D\\sql_oracle_temp.xml");
queryClob("C:\\D\\sql_oracle_temp.xml.txt");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void insertBlob(String fileName) {
Connection conn = getConnection();
PreparedStatement ps = null;
try {
String sql = "insert into sampledb.user (name, pswd, pic) values (?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "zhangsan");
ps.setString(2, "111");
// 设置二进制参数
File file = new File(fileName);
InputStream in = new BufferedInputStream(new FileInputStream(file));
ps.setBinaryStream(3, in, (int) file.length());
ps.executeUpdate();
in.close();
System.out.println("finished");
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void insertClob(String fileName) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = null;
try {
String sql = "insert into sampledb.user (name, pswd, remark) values (?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "zhangsan");
ps.setString(2, "111");
//设置二进制参数
File file = new File(fileName);
InputStreamReader reader = new InputStreamReader(new FileInputStream(file));
ps.setCharacterStream(3, reader, (int) file.length());
ps.executeUpdate();
reader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
}
public static void queryBlob(String outFileName) {
Connection conn = getConnection();
PreparedStatement ps = null;
Statement stmt = null;
ResultSet rs = null;
try {
String sql = "select pic from user where id = 6";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
InputStream in = rs.getBinaryStream(1);
File file = new File(outFileName);
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.flush();
out.close();
in.close();
}
rs.close();
stmt.close();
System.out.println("finished query blob");
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void queryClob(String outFileName) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = null;
Statement stmt = null;
ResultSet rs = null;
try {
String sql = "select remark from sampledb.user where id =16";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next()) {
Reader reader = rs.getCharacterStream(1);
File file = new File(outFileName);
//OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file));
//OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file),"ISO-8859-1");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file),"GB2312");
char[] buff = new char[1024];
for (int i = 0; (i = reader.read(buff)) > 0;) {
writer.write(buff, 0, i);
}
writer.flush();
writer.close();
reader.close();
}
rs.close();
stmt.close();
System.out.println("end");
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
}
private static Connection getConnection() {
String jdbc_url = "jdbc:mysql://localhost/sampledb?useUnicode=true&characterEncoding=UTF-8";
String jdbc_user = "root";
String jdbc_password = "root";
String jdbc_driver = "com.mysql.jdbc.Driver";
Connection conn = null;
try {
Class.forName(jdbc_driver);
conn = DriverManager.getConnection(jdbc_url, jdbc_user,
jdbc_password);
} catch (ClassNotFoundException cne) {
cne.printStackTrace();
} catch (SQLException se) {
se.printStackTrace();
} finally {
}
return conn;
}
}
分享到:
相关推荐
NULL 博文链接:https://shihuan830619.iteye.com/blog/1662937
运用Java如何存取Oracle中的CLOB类型字段
CLOB、BLOB___CLOB与BLOB的区别
DELPHI的CLOB和BLOB存取处理
kettle通过java代码将数据库blob 字段抽取到本地文件
Mybatis 处理 CLOB、BLOB 类型数据
修改clob blob 字段 修改clob blob 字段 修改clob blob 字段
oracle中使用jdbc读写clob字段,很多细节介绍,内容全面。
Oracle导出Clob,Blob等大字段工具,自己写的工具,方便大家下载使用
一个完整的工程,主要功能为:spring+hbernate对clob、blob字段的处理 包括:数据脚本、典型的SSH框架处理,以及spring、hibernate数据批量保存等功能源码、所用到的lib包 数据环境为:oracle 10G 开发:Mycelipse5.1...
只需要安装oracle client在本地,然后配置好tns,输入连接名,用户名,密码,数据表的名称,文件名数据列名以及blob数据列名,就可以批量导出ORACLE数据库BLOB字段生成图片
很多朋友在操作oracle数据库的clob,blob字段时不知道怎么插入和使用,这里提供读写范例。
delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段delphi读写BLOB字段...
java读取oracle数据库中clob字段 把oracle数据库中字段类型为clob的字段值以字符串的形式读取出来
完整通过JAVA读写ORACLE中CLOB、BLOB字段的方法,提供数据库连接池的方法
简单写的一个小工具,把图片存入oracle中,按clob和blob两种方式存储,并读取图片
oracle的jdbc驱动程序,用这个版本的操作Clob,blob类型的数据很方便。 博文链接:https://zhenjw.iteye.com/blog/173419
java中(注解符)处理Clob(blob)类型
Java存取OracleBlob字段,图片存储,Blob和BLOB的问题,Clob,oracle