JDBC学习之路(四)大文本和二进制文件的写入

18 篇文章 0 订阅

   老早就纳闷,每次使用MYSQL都是放一些小文本,小东西,不是听说数据库都是很牛逼的,能放灰常大的东西嘛?现在终于知道了向MYSQL中放入大文本大图片或者大二进制的方法了,不过貌似MYSQL对单个文件的放入有些限制,你得学会更改MYSQL的my.ini配置文件才行.

废话不多说,下面上代码,首先是大文本的写入

package com.bird.jdbc;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @use 在Mysql数据库中建立一个TEXT字段用来存放大文本
 * @author bird
 *
 */
public class InsertBigText {
	public static void insertBigText() throws SQLException, IOException{//插入大文本
		Connection conn = null;
		PreparedStatement pt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConnecttion();// 建立连接
			
			File file = new File("d:\\test.txt");
			Reader reader = new BufferedReader(new FileReader(file));//使用两层包装

			String sql = "insert into big_text(big) values (?)";// 问号代替num
			pt = conn.prepareStatement(sql);// 创建语句
			pt.setCharacterStream(1, reader);//使用字符流进行传输文本

			int i = pt.executeUpdate();// 执行语句
			System.out.println(i);
			reader.close();
		} finally {
			JdbcUtil.free(rs, pt, conn);
		}

	}
	
	
	public static void read() throws SQLException, IOException{//将数据读出来
		Connection conn = null;
		PreparedStatement pt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConnecttion();// 建立连接

			String sql = "select big from big_text ";// 问号代替num
			pt = conn.prepareStatement(sql);// 创建语句

			rs = pt.executeQuery();// 执行语句

			while (rs.next()) {// 得到数据
				Reader reader = rs.getCharacterStream(1);//重点
				File file = new File("d:\\1.txt");
				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.close();//一定要关闭,否则不刷新缓冲区,数据读取不全
				reader.close();
			}
		} finally {
			JdbcUtil.free(rs, pt, conn);
		}
	}
	
	public static void main(String [] args) throws SQLException, IOException{
		insertBigText();
		//read();
	}
}

1.注意,本人是事先在MYSQL中建立好了一个表,表名在代码中写了,创建存放大文本的字段时候一定要注意,一定要使用TEXT类型,如果你觉得小,就使用LONGTEXT,我就是使用LONGBLOB,详情查看MYSQL手册。


下面写二进制文件的写入

package com.bird.jdbc;

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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class BlobTest {

	/**
	 * @use 存放图片,数据库设置为LONGBLOB
	 * @author bird
	 * @throws IOException
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException, IOException {
		//create();
		read();
	}

	static void read() throws SQLException, IOException {//读取字节流写入文件
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			// 2.建立连接
			conn = JdbcUtil.getConnecttion();
			// conn = JdbcUtilsSing.getInstance().getConnection();
			// 3.创建语句
			st = conn.createStatement();

			// 4.执行语句
			rs = st.executeQuery("select photo from blob_test");

			// 5.处理结果
			while (rs.next()) {
				// Blob blob = rs.getBlob(1);
				// InputStream in = blob.getBinaryStream();
				InputStream in = rs.getBinaryStream("photo");

				File file = new File("d:\\IMG_0002_bak.jpg");
				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.close();//记住,一定要关闭
				in.close();
			}
		} finally {
			JdbcUtil.free(rs, st, conn);
		}
	}

	static void create() throws SQLException, IOException {//向数据库中写入图片
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			// 2.建立连接
			conn = JdbcUtil.getConnecttion();
			// conn = JdbcUtilsSing.getInstance().getConnection();
			// 3.创建语句
			String sql = "insert into blob_test(photo) values (?) ";
			ps = conn.prepareStatement(sql);
			File file = new File("d:\\Lighthouse.jpg");
			InputStream in = new BufferedInputStream(new FileInputStream(file));

			ps.setBinaryStream(1, in, (int) file.length());
			// 4.执行语句
			int i = ps.executeUpdate();

			in.close();

			System.out.println("i=" + i);
		} finally {
			JdbcUtil.free(rs, ps, conn);
		}
	}
}

1.我第一次插入图片的时候很悲催的失败了,说什么数据库版本不符合,扯淡。


2.提前在数据库中插入表,字段为LONGBLOB


3.注意,如果mysql是gbk编码的要在mysql安装目录下的配置文


件my.ini将字符集设置Latin1,输入图片后再改回来,否则无法插


入正确。


4.很多类型的单个最大文件插入上线都可以在配置文件中更改


5多查一查Mysql手册


注意如果mysql是gbk编码的要先把mysql的字符集设置Latin1,输入完图片后再设回来


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值