JDBC操作MySQL Lob字段记实
NAME varchar(24) DEFAULT NULL,
TXT text,
IMG blob
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
import java.sql.*;
import java.io.*;
/**
* JDBC 读取MySQL lob字段测试
* File: TestLob4MySQL.java
* User: leizhimin
* Date: 2008-3-3 14:44:30
*/
public class TestLob4MySQL {
public static final String url = "jdbc:mysql://localhost/testdb";
public static final String username = "root";
public static final String password = "leizhimin";
public static final String driverClassName = "com.mysql.jdbc.Driver";
/**
* 数据库连接获取器
*
* @return 数据库连接
*/
public static Connection makeConnection() {
Connection conn = null;
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
/**
* 测试数据库连接
*/
public static void testConnection() {
Connection conn = makeConnection();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mysql.user");
while (rs.next()) {
String s1 = rs.getString(1);
System.out.println(s1);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 插入Lob字段
*/
public static void testInsertlob() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(false);
File txtFile = new File("C:\\txt.txt");
File imgFile = new File("C:\\img.png");
int txt_len = (int) txtFile.length();
int img_len = (int) imgFile.length();
try {
InputStream fis1 = new FileInputStream(txtFile);
InputStream fis2 = new FileInputStream(imgFile);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO T_LOB(NAME,TXT,IMG) VALUES('G',?,?)");
pstmt.setAsciiStream(1, fis1, txt_len);
pstmt.setBinaryStream(2, fis2, img_len);
pstmt.executeUpdate();
conn.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 读取lob字段
*/
public static void testQueryLob() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT TXT,IMG FROM T_LOB");
int i = 1;
while (rs.next()) {
Clob clob = rs.getClob("TXT");
Blob blob = rs.getBlob("IMG");
InputStream txtIs = rs.getAsciiStream("TXT");
InputStream imgIs = rs.getBinaryStream("IMG");
InputStreamReader txtIsr = new InputStreamReader(txtIs);
InputStreamReader imgIsr = new InputStreamReader(imgIs);
BufferedReader buff_txtIsr = new BufferedReader(txtIsr);
BufferedReader buff_imgIsr = new BufferedReader(imgIsr);
String line = null;
while (null != (line = buff_txtIsr.readLine())) {
System.out.println(line); //将其输出至屏幕,实际你可以按照需要处理
}
File fileOutput = new File("c:\\img_x" + i + ".png");
FileOutputStream fo = new FileOutputStream(fileOutput);
int c;
while ((c = imgIs.read()) != -1)
fo.write(c);
fo.close();
System.out.println("img " + i + " retrieved!");
i++;
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 读取lob字段
*/
public static void testQueryLob1() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT TXT,IMG FROM T_LOB");
while (rs.next()) {
Clob clob = rs.getClob("TXT");
Blob blob = rs.getBlob("IMG");
InputStream txtIs = clob.getAsciiStream();
InputStream imgIs = blob.getBinaryStream();
InputStreamReader txtIsr = new InputStreamReader(txtIs);
InputStreamReader imgIsr = new InputStreamReader(imgIs);
BufferedReader buff_txtIsr = new BufferedReader(txtIsr);
BufferedReader buff_imgIsr = new BufferedReader(imgIsr);
String line = null;
while (null != (line = buff_txtIsr.readLine())) {
System.out.println(line); //将其输出至屏幕,实际你可以按照需要处理
}
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 删除lob字段
*/
public static void testDeleteLob() {
Connection conn = makeConnection();
try {
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
int row = stmt.executeUpdate("DELETE FROM T_LOB");
conn.commit();
System.out.println("删除 " + row + " 行数据!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 读取lob字段
*/
public static void testUpdateLob() {
Connection conn = makeConnection();
try {
String in_str="HAHAHAHAHAHA!!!";
File in_file=new File("c:\\img_haha.png");
InputStream txt_is = string2InputStream(in_str);
InputStream img_is =new FileInputStream(in_file);
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement("UPDATE T_LOB SET TXT=?, IMG=? WHERE NAME='G'");
pstmt.setAsciiStream(1,txt_is,in_str.getBytes().length);
pstmt.setBinaryStream(2,img_is,(int)in_file.length());
int row = pstmt.executeUpdate();
conn.commit();
txt_is.close();
img_is.close();
// System.out.println("更新 " + row + " 行数据!");
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
// testInsertlob();
// testQueryLob();
// testQueryLob1();
// testDeleteLob();
testUpdateLob();
}
public static InputStream string2InputStream(String str) {
if (str == null) return null;
return new ByteArrayInputStream(str.getBytes());
}
public static String inputStream2String(InputStream is) {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String inputLine;
try {
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}
JDBC操作MySQL Lob字段记实相关推荐
- Java使用Jdbc操作MySql数据库(一)
这个示例是Java操作MySql的基本方法. 在这个示例之前,要安装好MySql,并且配置好账户密码,创建一个logininfo数据库,在数据库中创建userinfo数据表.并且在表中添加示例数据. ...
- Java 操作MySql Blob 字段
Java 操作MySql Blob 字段 import java.sql.*; import java.io.*; import javax.swing.*; import java.awt.*; i ...
- JDBC学习总结(二)JDBC操作Blob类型字段高效的批量插入JDBC处理数据库事务将多个SQL看成一个事务执行数据库连接池C3P0DBCPDruidDBUtils工具类实现CRUD
JDBC学习总结(二)JDBC操作Blob类型字段/高效的批量插入/JDBC处理数据库事务/将多个SQL看成一个事务执行/数据库连接池C3P0/DBCP/Druid/DBUtils工具类实现CRUD ...
- 【Java数据库】使用JDBC操作MySQL数据库、Batch批处理 、事务的概念
MySQL 数据库的命令行操作 登陆操作mysql -hlocalhost –uroot –p123456 退出操作exit 数据库操作建库:create database 库名; 卸载库:drop ...
- [Java]JDBC操作MySQL数据库
public class MysqlConnect {//定义MySQL数据库的连接地址public static final String DBURL = "jdbc:mysql://ip ...
- Java中通过JDBC操作MySQL数据库
JDBC相关的操作 0.JDBC常用类和接口介绍 DriverManager类 DriverManager类用来管理数据库中的所有驱动程序:是JDBC的管理层,作用于用户和驱动程序之间,跟踪可用 ...
- 使用jdbc操作mysql数据库_java使用JDBC操作MYSQL数据库
1.新建数据库 DROP TABLE IF EXISTS `test`.`user`; CREATE TABLE `test`.`user` ( `username` varchar(45) DEFA ...
- 使用JDBC操作MySQL数据库
我们用大学的四年去怀念高中的三年,但是我们会用大学以后的余生去怀念大学的四年. 1.JDBC 概述 JDBC(Java Database Connectivity),Java数据库连接. sun公司为 ...
- c mysql封装 jdbc_彻底封装JDBC操作MySQL的连接。
只需3步. 1.导入mysql的jar包. 略 2.封装配置文件db.properties driverClass = com.mysql.jdbc.Driver url = jdbc:mysql:/ ...
最新文章
- BIND 子域授权的实现和区域转发实现
- 负载均衡器之 Haproxy
- Android开发过程中内存泄露检测
- linux三剑客试题,linux三剑客-awk
- 帆软报表(finereport)单元格函数,OP参数
- 历史回顾——NLP问题解决方案的演变史
- piblog 0.1
- 安装Node和NPM
- 力扣题目——566. 重塑矩阵
- 听说你还不理解JavaScript闭包
- 惠普服务器win10安装系统教程,惠普电脑如何重装系统教程 win10专业版系统安装教程...
- Webpack4 配置 Output
- Python项目文件引用问题:ImportError: attempted relative import with no known parent package
- acpc2013 G. The Stones Game (思维)
- Atitit 华为管理者内训书系 以奋斗者为本 华为公司人力资源管理纲要 attilax读后感...
- 电机驱动电路之H桥基本知识---Trinamic电机驱动芯片
- 这所北京计算机类大学,改考408了!北京信息科技大学
- Qt学习之添加游戏音效
- 【菜鸟窝出品】 python的变量和逻辑基础(python数据分析入门)
- mysql4.0事务_聊一聊 MySQL 中的事务及其实现原理