- 浏览: 133830 次
- 性别:
- 来自: 杭州
最新评论
-
云散风清lc:
水电费分散三大方法是[color=red][/color]
zkClient -
fireye83:
分开打包上传的,文件大小有限制,一个文件只能小于10M
android-sdk-window与ADT下载 -
kkppccdd:
Alumnus
Self-introduction template -
louieyu:
android-7的各个部分是不是一样的啊?来个说明啊
android-sdk-window与ADT下载 -
q694119254:
...
android 反编译心得
MYSQ数据库CLOB、BLOB存取
- 博客分类:
- java
文本大字段类型(CLOB)
------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>> 数据库脚本 <<<<<<<<<<
- CREATE TABLE `clob_test` (
- `id` integer NOT NULL AUTO_INCREMENT ,
- `txt` text NOT NULL ,
- PRIMARY KEY (`id`)
- )
- ;
CREATE TABLE `clob_test` ( `id` integer NOT NULL AUTO_INCREMENT , `txt` text NOT NULL , PRIMARY KEY (`id`) ) ;
>>>>>>>>>> ClobTest.java <<<<<<<<<<
- package com.test;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Reader;
- import java.io.Writer;
- import java.sql.Clob;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ClobTest {
- public static void main(String[] args) throws SQLException, IOException {
- read();
- System.out.println("--- End ---");
- }
- // 增加
- static void create() throws SQLException, IOException {
- Connection conn = null;
- PreparedStatement pstmt = null;
- String sql = "insert into clob_test (txt) values (?)";
- try {
- conn = JdbcUtils.getConnection();
- pstmt = conn.prepareStatement(sql);
- File file = new File("src/com/test/JdbcUtils.java");
- Reader reader = new BufferedReader(new FileReader(file));
- pstmt.setCharacterStream(1, reader, file.length());
- pstmt.executeUpdate();
- reader.close();
- } finally {
- JdbcUtils.free(null, pstmt, conn);
- }
- }
- // 查询
- static void read() throws SQLException, IOException {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- String sql = "select txt from clob_test";
- try {
- conn = JdbcUtils.getConnection();
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- Clob clob = rs.getClob(1);
- Reader reader = clob.getCharacterStream();
- 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.flush();
- writer.close();
- reader.close();
- }
- } finally {
- JdbcUtils.free(rs, stmt, conn);
- }
- }
- }
package com.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.sql.Clob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class ClobTest { public static void main(String[] args) throws SQLException, IOException { read(); System.out.println("--- End ---"); } // 增加 static void create() throws SQLException, IOException { Connection conn = null; PreparedStatement pstmt = null; String sql = "insert into clob_test (txt) values (?)"; try { conn = JdbcUtils.getConnection(); pstmt = conn.prepareStatement(sql); File file = new File("src/com/test/JdbcUtils.java"); Reader reader = new BufferedReader(new FileReader(file)); pstmt.setCharacterStream(1, reader, file.length()); pstmt.executeUpdate(); reader.close(); } finally { JdbcUtils.free(null, pstmt, conn); } } // 查询 static void read() throws SQLException, IOException { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select txt from clob_test"; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { Clob clob = rs.getClob(1); Reader reader = clob.getCharacterStream(); 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.flush(); writer.close(); reader.close(); } } finally { JdbcUtils.free(rs, stmt, conn); } } }
二进制大字段类型(BLOB)
------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>> 数据库脚本 <<<<<<<<<<
- CREATE TABLE `blob_test` (
- `id` integer NOT NULL AUTO_INCREMENT ,
- `pic` blob NOT NULL ,
- PRIMARY KEY (`id`)
- )
- ;
CREATE TABLE `blob_test` ( `id` integer NOT NULL AUTO_INCREMENT , `pic` blob NOT NULL , PRIMARY KEY (`id`) ) ;
- package com.test;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.sql.Blob;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class BlobTest {
- public static void main(String[] args) throws SQLException, IOException {
- read();
- System.out.println("--- End ---");
- }
- // 增加
- static void create() throws SQLException, IOException {
- Connection conn = null;
- PreparedStatement pstmt = null;
- String sql = "insert into blob_test (pic) values (?)";
- try {
- conn = JdbcUtils.getConnection();
- pstmt = conn.prepareStatement(sql);
- File file = new File("level.gif");
- InputStream is = new BufferedInputStream(new FileInputStream(file));
- pstmt.setBinaryStream(1, is, file.length());
- pstmt.executeUpdate();
- is.close();
- } finally {
- JdbcUtils.free(null, pstmt, conn);
- }
- }
- // 查询
- static void read() throws SQLException, IOException {
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- String sql = "select pic from blob_test";
- try {
- conn = JdbcUtils.getConnection();
- stmt = conn.createStatement();
- rs = stmt.executeQuery(sql);
- while (rs.next()) {
- Blob blob = rs.getBlob(1);
- InputStream is = blob.getBinaryStream();
- File file = new File("logo.gif");
- OutputStream os = new BufferedOutputStream(
- new FileOutputStream(file));
- byte[] buff = new byte[1024];
- for (int i = 0; (i = is.read(buff)) > 0;) {
- os.write(buff, 0, i);
- }
- os.flush();
- os.close();
- is.close();
- }
- } finally {
- JdbcUtils.free(rs, stmt, conn);
- }
- }
- }
package com.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class BlobTest { public static void main(String[] args) throws SQLException, IOException { read(); System.out.println("--- End ---"); } // 增加 static void create() throws SQLException, IOException { Connection conn = null; PreparedStatement pstmt = null; String sql = "insert into blob_test (pic) values (?)"; try { conn = JdbcUtils.getConnection(); pstmt = conn.prepareStatement(sql); File file = new File("level.gif"); InputStream is = new BufferedInputStream(new FileInputStream(file)); pstmt.setBinaryStream(1, is, file.length()); pstmt.executeUpdate(); is.close(); } finally { JdbcUtils.free(null, pstmt, conn); } } // 查询 static void read() throws SQLException, IOException { Connection conn = null; Statement stmt = null; ResultSet rs = null; String sql = "select pic from blob_test"; try { conn = JdbcUtils.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { Blob blob = rs.getBlob(1); InputStream is = blob.getBinaryStream(); File file = new File("logo.gif"); OutputStream os = new BufferedOutputStream( new FileOutputStream(file)); byte[] buff = new byte[1024]; for (int i = 0; (i = is.read(buff)) > 0;) { os.write(buff, 0, i); } os.flush(); os.close(); is.close(); } } finally { JdbcUtils.free(rs, stmt, conn); } } }
数据库工具类:
------------------------------------------------------------------------------------------------------------------------
>>>>>>>>>> JdbcUtils.java <<<<<<<<<<
- package com.test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- * 数据库工具类
- *
- * @author MY
- * @version 1.0, 2011-03-03
- *
- */
- public final class JdbcUtils {
- private static String url = "jdbc:mysql:///jdbc";
- private static String username = "root";
- private static String password = "";
- private JdbcUtils() {
- }
- static {
- try {
- // 1、注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new ExceptionInInitializerError(e);
- }
- }
- /**
- * 得到数据库连接
- *
- * @return
- * @throws SQLException
- */
- public static Connection getConnection() throws SQLException {
- return DriverManager.getConnection(url, username, password);
- }
- /**
- * 关闭数据库资源
- *
- * @param rs
- * @param stmt
- * @param conn
- */
- public static void free(ResultSet rs, Statement stmt, Connection conn) {
- try {
- if (rs != null) {
- rs.close();
- rs = null;
- }
- } catch (SQLException e) {
- System.out.println("ResultSet关闭失败:" + e.getMessage());
- } finally {
- try {
- if (stmt != null) {
- stmt.close();
- stmt = null;
- }
- } catch (SQLException e) {
- System.out.println("Statement关闭失败:" + e.getMessage());
- } finally {
- try {
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (SQLException e) {
- System.out.println("Connection关闭失败:" + e.getMessage());
- }
- }
- }
- }
- }
package com.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库工具类 * * @author MY * @version 1.0, 2011-03-03 * */ public final class JdbcUtils { private static String url = "jdbc:mysql:///jdbc"; private static String username = "root"; private static String password = ""; private JdbcUtils() { } static { try { // 1、注册驱动 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } /** * 得到数据库连接 * * @return * @throws SQLException */ public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } /** * 关闭数据库资源 * * @param rs * @param stmt * @param conn */ public static void free(ResultSet rs, Statement stmt, Connection conn) { try { if (rs != null) { rs.close(); rs = null; } } catch (SQLException e) { System.out.println("ResultSet关闭失败:" + e.getMessage()); } finally { try { if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { System.out.println("Statement关闭失败:" + e.getMessage()); } finally { try { if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { System.out.println("Connection关闭失败:" + e.getMessage()); } } } } }
>>>>>>>>>> (单例模式)JdbcUtilsSing.java <<<<<<<<<<
- package com.test;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- /**
- * 数据库工具类<br />
- * 单例模式
- *
- * @author MY
- * @version 1.0, 2011-03-03
- *
- */
- public final class JdbcUtilsSing {
- private String url = "jdbc:mysql:///jdbc";
- private String username = "root";
- private String password = "";
- private static JdbcUtilsSing instance = null;
- private JdbcUtilsSing() {
- }
- public static JdbcUtilsSing getInstance() {
- if (instance == null) {
- synchronized (JdbcUtilsSing.class) {
- if (instance == null) {
- instance = new JdbcUtilsSing();
- }
- }
- }
- return instance;
- }
- static {
- try {
- // 1、注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new ExceptionInInitializerError(e);
- }
- }
- /**
- * 得到数据库连接
- *
- * @return
- * @throws SQLException
- */
- public Connection getConnection() throws SQLException {
- return DriverManager.getConnection(url, username, password);
- }
- /**
- * 关闭数据库资源
- *
- * @param rs
- * @param stmt
- * @param conn
- */
- public void free(ResultSet rs, Statement stmt, Connection conn) {
- try {
- if (rs != null) {
- rs.close();
- rs = null;
- }
- } catch (SQLException e) {
- System.out.println("ResultSet关闭失败:" + e.getMessage());
- } finally {
- try {
- if (stmt != null) {
- stmt.close();
- stmt = null;
- }
- } catch (SQLException e) {
- System.out.println("Statement关闭失败:" + e.getMessage());
- } finally {
- try {
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (SQLException e) {
- System.out.println("Connection关闭失败:" + e.getMessage());
- }
- }
- }
- }
- }
http://blog.csdn.net/prstaxy/article/details/8206802
发表评论
-
zooKeeper在weblogic下的bug
2014-12-18 17:24 3108因为项目中需要使用分 ... -
zkClient
2013-11-03 21:50 8330直接使用zk的api实现业务功能比较繁琐。因为要处理sess ... -
I0Itec-zkClient小结
2013-11-03 17:17 850zookeeper客户端目前社区中比较多,而且大家在使用zo ... -
统一权限目的
2013-05-28 12:35 01.已经拥有了一套基于角色的系统,(IVMS8200 ... -
Java开发中的Memcache原理及实现
2013-01-10 10:56 777七、 Memcached 客户端程序 ... -
ant ftp 大文件上传不上去问题
2013-01-06 13:58 1095ant支持ftp上传需要commons-net的jar包支持, ... -
2011-07-13 19:43 java 如何检测内存泄漏
2012-12-25 13:54 796http://hi.baidu.com/l_rigidity/ ... -
大对象数据的的处理clob和blob
2012-12-19 13:47 01、什么是clob和blob? ... -
java存取oracle中的COLB类型数据
2012-12-19 11:55 902java存取oracle中的COLB类型数据 http:// ... -
hibernat批量处理和存取大对象
2012-12-19 11:48 804http://wenku.baidu.com/view/6f6 ... -
String 、InputStream、Reader 之间的转换
2012-12-19 11:01 9081、String –> InputStreamInput ... -
M2eclipse与tomcatplugin布署使用
2012-10-11 13:43 1941M2eclipse与tomcatplugin布 ... -
Batch update returned unexpected row count from update [2]; actual row count
2012-08-09 15:49 4620Batch update returned unexpecte ... -
集群中几种session同步解决方案的比较
2012-06-12 17:12 6384集群中session安全和同步是个最大的问题,下面是我收集 ... -
Hibernate和Mysql5.5创建表出错——type=InnDB
2012-06-11 10:50 1075今天把以前做的一个项目迁移到自己的机器上,项目使用SSH。 ... -
使用 生成runnable jar
2012-06-08 09:12 2176<?xml version="1.0" ... -
tomcat 增大内存
2012-05-28 15:33 830很大的web工程,用tomcat默认分配的内存空间无法启动,如 ... -
sql性能监控(p6spy使用)
2012-05-05 17:50 794http://xiongzhenhui.iteye.com/b ... -
提高hibernate性能
2012-05-05 17:43 715http://blog.csdn.net/tianyawudi ... -
Hibernate之性能监测方法与工具
2012-05-05 17:41 723http://peirenlei.iteye.com/blog ...
相关推荐
学生成绩管理系统数据库mysq数据库学生成绩管理系统数据库mysq数据库
JDBC连接mysq数据库Demo
汉字转的拼库 mysq数据库版本 汉字转的拼库 mysq数据库版本
mysq数据库高级笔记
最新安装板Mysql数据库,功能更强大,更好用,亲测真实可用。
怎文档可以教授你怎么使用命令行提示符来操作Mysql数据库
在这可以爆料社会上一切对农民工不关的事,农民工可以在上面请求帮助,还可以在上面发布招工信息,和查找工作等功能
10万单词库sql
一、连接数据库的参数:MYSQ数据库IP、MYSQ数据库用户名、MYSQ数据库名、MYSQ数据库密码 二、设置要/充值表名/ 例如:发卡_net 三、设置/充值用户字段名,也就是客户充值时要输入的例如 用户名 或 ID 四、设置/充值...
此项目是利用struts2框架写的简单BBS模型,数据库利用mysql,很适合初学者参考。
《MySQL数据库入门》教学设计 课程名称: MySQL开发入门教程 授课年级: XXXX年级 授课学期: XXXX学年第一学期 教师姓名: 某某老师 《MySQL数据库入门》教学设计全文共7页,当前为第1页。2014年09月09日 《MySQL...
数据库课程设计 库存管理 登记入库的药品。 登记出库的药品。 每日检查库存下限,报警。 每日检查过期的药品,报警并做退回销毁处理。 对有问题的药品的退回供应商。记录退回的药品的名称、数量、金额和退货原因等...
使用 jsp 和servlet 和mysql 实现的网络同学录,可以实现注册,登陆 同学的显示,头像的修改,个性签名的修改,和对访问界面的留言
1.自动识别数据库版本,对于MySQL 4.1以上,备份数据时提示选择字符集. 2.导入数据时,提示原数据库编码,并自动识别. 3.增加导入目标数据库字符集选项. 4.支持GBK、BIG5、UTF8之间的编码转换(见特别说明4). 特别...
所有行业分类,全国各省、市、县,招聘要求,福利待遇等等等等SQL,执行就可生成表,使用方便
MySQL数据库安装图解 下面的是MySQL安装的图解,用的可执行文件安装的,详细说明了一下! 打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行“setup.exe”,出现如下界面 mysql安装向导启动,按...
VB.NET 操作mysql数据库 使用到了一个dll文件,可以网上找下去下载。下载后有说明。如果不明的可以邮箱联系x@zucms.com。 没有接触过vb.net或者连怎么运行都不知道的就不要联系了。
MySQL实用命令大全
mysql数据库备份程序 mysq备份程序 文件小 只需要简单配置数据库连接信息即可方便的导出mysq数据库为sql文件
省市县联动,dwr hibernate, mysql, 请先导入sql文件,配置好数据库,就可以了