mysql读取大文件_jdbc往mysql读取大文本文件

该博客演示了如何使用Java JDBC操作大文本文件。首先,它展示了如何从本地文件读取内容并将其插入到MySQL的CLOB字段中。接着,它详细解释了如何从数据库中查询含有大文本的数据,并将内容写入到本地硬盘的文件中。整个过程通过JdbcUtil工具类连接数据库,利用PreparedStatement执行SQL语句。
摘要由CSDN通过智能技术生成

package jdbc;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.Reader;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import org.junit.Test;

import utils.JdbcUtil;

public class TestClob {

//读取本地文件写到数据库

@Test

public void add() {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try {

conn = JdbcUtil.getConnection();

String sql = "insert into testclob(content) values(?)";

st = conn.prepareStatement(sql);

String path = TestClob.class.getClassLoader().getResource("1.txt").getPath();

File file = new File(path);

st.setCharacterStream(1, new FileReader(file), file.length());

int num = st.executeUpdate();

if (num > 0) {

System.out.println("插入成功!!!");

}

} catch (Exception e) {

throw new RuntimeException(e);

} finally {

JdbcUtil.release(conn, st, rs);

}

}

//读取文件写到本地硬盘

@Test

public void read(){

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtil.getConnection();

String sql = "select content from testclob where id = 3";

st = conn.prepareStatement(sql);

rs = st.executeQuery();

if(rs.next()){

FileWriter out = new FileWriter("d:\\1.java");

Reader reader = rs.getCharacterStream("content");

char buffer[]= new char[1024];

int len = 0;

while((len=reader.read(buffer))>0){

out.write(buffer, 0, len);

}

out.close();

reader.close();

}

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtil.release(conn, st, rs);

}

}

}//工具类package utils;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Properties;

public class JdbcUtil {

private static String password;

private static String username;

private static String url;

static {

try {

InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");

Properties pr = new Properties();

pr.load(in);

username=pr.getProperty("username");

url = pr.getProperty("url");

password = pr.getProperty("password");

String driver = pr.getProperty("driver");

Class.forName(driver);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public static Connection getConnection() throws SQLException {

return DriverManager.getConnection(url, username, password);

}

public static void release(Connection conn, PreparedStatement st, ResultSet rs) {

if(rs!=null){

try{

rs.close();

}catch (Exception e){

throw new RuntimeException(e);

}

rs = null;

}

if(st != null){

try{

st.close();

}catch (Exception e){

throw new RuntimeException(e);

}

st = null;

}

if(conn != null){

try{

conn.close();

}catch (Exception e){

throw new RuntimeException(e);

}

conn = null;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值