java 写入mysql blob_java 读取写入 mysql blob
2019独角兽企业重金招聘Python工程师标准>>>
package com.sohu.smc.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class MySqlTest {
static String str = "中国Chinaasdsaiuhdiusankjxnbsiudisabxisaiuaiushxnaiuiusax";
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
}
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(
str.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平台默认编码,也可以显式的指定如toString("GBK")
return out.toString();
}
static void query() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql:///", "",
"");
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from test");
while (rs.next()) {
ByteArrayInputStream strtianliang = (ByteArrayInputStream) rs
.getBlob("desc0").getBinaryStream();
byte[] byte_data = new byte[strtianliang.available()];
strtianliang.read(byte_data, 0, byte_data.length);
String result = new String(byte_data);
System.out.println("result uncompress :" + uncompress(result));
}
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
static void insert() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"");
PreparedStatement st = null;
String str0 = compress(str);
String sql = "insert into test(desc0) values(?)";
st = conn.prepareStatement(sql);
byte[] str0s = str0.getBytes();
InputStream bastr = new ByteArrayInputStream(str0s);
st.setBinaryStream(1, bastr, str0s.length);
st.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
conn = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws Exception {
insert();
System.out.println("-----------------------------------");
query();
}
}
java 写入mysql blob_java 读取写入 mysql blob相关推荐
- java 读取dwg_jdwglib java dwg文件的读取,写入开发包. dwg使用当前 常方便,测试代码和jar都有 CAD 247万源代码下载- www.pudn.com...
文件名称: jdwglib下载 收藏√ [ 5 4 3 2 1 ] 所属分类: CAD 开发工具: Java 文件大小: 608 KB 上传时间: 2015-12-08 下载次数: 0 提 ...
- Java NIO 读取文件、写入文件、读取写入混合
前言 Java NIO(new/inputstream outputstream)使用通道.缓冲来操作流,所以要深刻理解这些概念,尤其是,缓冲中的数据结构(当前位置(position).限制(limi ...
- java如何将mp4写入光盘_iOS - 读取/写入mp4视频的XMP元数据
我需要在mp4容器中读取并注入XMP元数据 . 我知道这可以在Android上使用"mp4parser"库,但我找不到iOS的等价物 . 对于读取部分,是否可以从相机胶卷读取每个素 ...
- java excel row遍历空_Java poi读取,写入Excel,处理row和cell可能为空的情况
首先需要导入包 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NP ...
- java写入dat文件_java写入dat文件
Java的输入输出包括字节流.文件流.对象流等,要注意区分不同流使用的不同类.字... Java的输入输出包括字节流.文件流.对象流等,要注意区分不同流使用的不同类.字... Java的输入输出包括字 ...
- NFC读取写入简单使用
NFC读取写入简单使用 适用于无加密普通NFC卡片数据读取写入 NFC数据读取写入的方法 Demo地址:https://download.csdn.net/download/st526403649/1 ...
- java向mysql写入数据慢_通过java代码往mysql数据库中写入日期相关数据少13个小时...
通过show variables like '%time_zone%'; 查看时区: CST 时区 名为 CST 的时区是一个很混乱的时区,有四种含义: 美国中部时间 Central Standard ...
- php怎么从文件中读取数据库连接,PHP连接 读取 写入mysql数据库的方法 附常用代码...
MYSQL一般都是和PHP配套使用用来建设网站,这里提供PHP连接MySQL数据库以及读取写入数据库的方法,供大家参考使用: 1.为了更好地设置数据连接,一般会将数据连接所涉及的值定义成变量. $my ...
- SpringBoot连接mysql数据,写入数据
(1)先准备好mysql数据,作为springboot的数据存储服务器. 安装和启动mysql服务器的介绍:https://zhangphil.blog.csdn.net/article/detail ...
最新文章
- oracle 全局临时变量,如何解析Oracle PL / SQL中的简单XML片段并将其加载到全局临时表中?...
- 经典br = new BufferedReader(new InputStreamReader(process.getInputStream()));
- Mysql用navicat查看建表语句
- php超市结算,超市物品结算简易程序代码
- win7系统0x0000001a蓝屏代码怎么办
- mysql中用来取余数的函数是_Excel中一个专门用来评分的函数TRIMMEAN
- 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(2)
- 半天的成果,已经很快了,还有方案实现过程
- Nodejs在Debian和Ubuntu上安装
- 取消开机就弹出 msn中文网 操作步骤
- 计算机行业未来作文,未来的计算机作文
- 如何在知网直接下载PDF格式的硕博士论文
- GAN 模式崩溃的理论解释
- 计算机宏应用实例,【电脑技巧】Office word宏命令巧妙应用一例
- 泛型(泛型类、泛型方法、泛型接口、通配符)详解
- web前端期末大作业 基于HTML+CSS家乡主题毕业设计源码
- 计算机基础我的课堂调控,计算机基础课堂析及实验安排.doc
- OS-S1 操作系统概论
- macOS 安装 brew步骤
- visual stdio 2015社区版、专业版的下载
热门文章
- webstorm2018修改运行web page端口号,并且让web在本地局域网内用IP访问
- 在CentOS 7.7 x86_64上安装python3.7.7
- vim粘帖的一个问题分析(pastetoggle)
- java并发编程实战:第十六章----Java内存模型
- VirtualBox - RTR3InitEx failed with rc=-1912 (rc=-1912)
- 20162325 金立清 S2 W8 C17
- Windows搭建以太坊的私有链环境
- Spark2 ML 学习札记
- 012-python基础-数据运算
- Intent携带额外的数据的方法