java 读取写入 mysql blob

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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();


}

}


转载于:https://my.oschina.net/u/1388024/blog/303815

你可能感兴趣的:(java 读取写入 mysql blob)