11_传智播客JDBC_用jdbc访问大段文本数据
2009-04-26 15:01
585 查看
快捷键 F3 = ctrl+鼠标左键
package five.base;
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.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import five.utils.Utils;
// 快捷键 F3 = ctrl+鼠标左键
public class BlobTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//create(3, "c:/1.txt");
read(3);
}
public static void create( int id, String fileName) throws IOException{
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = Utils.getConnection();
String sql = "insert into clob (id, test) values (?,?)";
ps = connection.prepareStatement(sql);
File file = new File(fileName);
//装饰模式 带有缓冲区 读取 效率高
Reader reader = new BufferedReader(new FileReader(file));
ps.setInt(1, id);
// 前提 Ascii
// parameterIndex - the first parameter is 1, the second is 2, ...
// x - the Java input stream that contains the ASCII parameter value
// length - the number of bytes in the stream
//ps.setAsciiStream(2, x, length);
// 字节流
ps.setCharacterStream(2, reader, (int)file.length());
int i = ps.executeUpdate();
reader.close();
System.out.println(i);
} catch (SQLException e) {
e.printStackTrace();
}finally{
Utils.free(connection, ps, rs);
}
}
static void read(int id) throws Exception {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = Utils.getConnection();
String sql = "select test from clob";
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
// 5 处理结果
while (rs.next()) {
//Clob clob = rs.getClob(1);
//Reader reader = clob.getCharacterStream();
String test = rs.getString(1);
System.out.println(test);
Reader reader = rs.getCharacterStream("test");
File file = new File("D:/cgz/dayByDayJdbc/Base_Bak.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 {
Utils.free(connection, ps, rs);
}
}
}
package five.base;
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.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import five.utils.Utils;
// 快捷键 F3 = ctrl+鼠标左键
public class BlobTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//create(3, "c:/1.txt");
read(3);
}
public static void create( int id, String fileName) throws IOException{
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = Utils.getConnection();
String sql = "insert into clob (id, test) values (?,?)";
ps = connection.prepareStatement(sql);
File file = new File(fileName);
//装饰模式 带有缓冲区 读取 效率高
Reader reader = new BufferedReader(new FileReader(file));
ps.setInt(1, id);
// 前提 Ascii
// parameterIndex - the first parameter is 1, the second is 2, ...
// x - the Java input stream that contains the ASCII parameter value
// length - the number of bytes in the stream
//ps.setAsciiStream(2, x, length);
// 字节流
ps.setCharacterStream(2, reader, (int)file.length());
int i = ps.executeUpdate();
reader.close();
System.out.println(i);
} catch (SQLException e) {
e.printStackTrace();
}finally{
Utils.free(connection, ps, rs);
}
}
static void read(int id) throws Exception {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = Utils.getConnection();
String sql = "select test from clob";
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
// 5 处理结果
while (rs.next()) {
//Clob clob = rs.getClob(1);
//Reader reader = clob.getCharacterStream();
String test = rs.getString(1);
System.out.println(test);
Reader reader = rs.getCharacterStream("test");
File file = new File("D:/cgz/dayByDayJdbc/Base_Bak.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 {
Utils.free(connection, ps, rs);
}
}
}
相关文章推荐
- jdbc访问大段文本数据
- 第十一章 用jdbc访问大段文本数据
- 用jdbc访问大段文本数据
- 12_传智播客JDBC_用jdbc访问二进制类型的数据
- jdbc_数据类型与日期问题_访问大段文本数据_二进制类型的数据
- JDBC保存、读取大文本数据类型
- 大数据,大文本的处理、JDBC啊!
- JDBC处理大数据、大文本、二进制数据、批处理相关知识
- 链接服务器 OLE DB 访问接口 'SQLNCLI10' 返回的数据与列 '[LINK_SCT1].[SCT].[dbo].[v_Employee_Company].empname' 所需的数据长度不匹配。所需的(最大)数据长度为 10,但返回的数据长度为 11。
- 带参数查询网页(JDBC-ODBC桥数据访问)
- JDBC演示向mysql中,存取大文本数据问题
- 使用Spring进行JDBC数据访问
- 如何在C程序中访问文本文档,并读取数据
- JDBC学习笔记(8):访问大段文本数据
- JDBC数据访问接口
- JDBC访问各个数据库
- spring boot demo(spring jdbc访问数据)
- 使用JDBC进行数据访问【spring-framework-reference】
- JDBC数据访问操作流程
- 【Effective Java】11、同步访问共享的可变数据