Mysql+TEXT(大文本文件)的存储和读取

package servletDemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTEXT{
	//在oracle里面大文本数据叫clob 在mysql里面叫做text 可以存放 .txt .doc 但是文件太大还是不能存
	
	private static final String URL = "jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC";
	private static final String USERNAME = "root";
	private static final String PWD = "root";
	public static void update(){//增删改
		Connection connection = null;//因为有可能还么有用到connection就报错了 那么这个对象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		try{
			//1导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2与DB建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3发送sql语句,执行(增删改)
			//pstmt需要预编译sql
			String sql = "insert into my_novel values (?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, 2);
			File file = new File("D:\\01.doc");
			InputStream in = new FileInputStream(file);
			//转换流可以设置编码
			Reader reader = new InputStreamReader(in,"UTF-8");
			pstmt.setCharacterStream(2, reader, (int)file.length());
	
			int count = pstmt.executeUpdate();//返回表示增删改 了几条数据
			//4处理结果集
			if(count>0){
				System.out.println("操作成功!");
			}
			reader.close();
			in.close();
		}catch (ClassNotFoundException e) {//Class.forName()的异常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()异常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是无论有没有异常都需要关闭连接
			try{
				if(pstmt!=null){//执行sql语句
					pstmt.close();
				}
				if(connection!=null){//建立java代码和数据库的连接
					connection.close();
				}
			}catch(SQLException e) {//前面两个close 都会抛SQLException异常
				e.printStackTrace();
			}
		}		
	}
	public static void query(){//增删改
		Connection connection = null;//因为有可能还么有用到connection就报错了 那么这个对象是空的就不能使用close方法
		PreparedStatement pstmt = null;//同理
		ResultSet rs= null;
		try{
			//1导入驱动,加载具体的驱动类
			Class.forName("com.mysql.cj.jdbc.Driver");
			//2与DB建立连接
			connection = DriverManager.getConnection(URL, USERNAME, PWD);
			//3发送sql语句,执行(查)
			//pstmt需要预编译sql
			String sql = "select novel from my_novel where id_novel = ?";
			pstmt = connection.prepareStatement(sql);
			//找出来编号为1 的 文件
			pstmt.setInt(1, 2);
			//执行 返回 表格
			rs = pstmt.executeQuery();
			//因为这次只有一个setXxxx getXxxx      setInt  getInt
			if(rs.next()){
				//列名叫做novel
				Reader reader = rs.getCharacterStream("novel");
				Writer writer = new FileWriter("src/02.doc");
				//Reader已经读取到内存中了 现在用Writer 写到硬盘上
				//这个是内存与硬盘中的缓冲区
				char [] chs = new char [100];
				int len = -1;
				while((len = reader.read(chs))!=-1){
					writer.write(chs,0,len);
				}
				writer.close();
				reader.close();
			}
 		}catch (ClassNotFoundException e) {//Class.forName()的异常
			e.printStackTrace();
		} catch (SQLException e) {//connection.createStatement()异常
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}finally{//用finally的原因是无论有没有异常都需要关闭连接
			try{
				if(pstmt!=null){//执行sql语句
					pstmt.close();
				}
				if(connection!=null){//建立java代码和数据库的连接
					connection.close();
				}
			}catch(SQLException e) {//前面两个close 都会抛SQLException异常
				e.printStackTrace();
			}
		}		
	}

	public static void main(String [] args){
//		update();
		query();
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章