Incorrect string value: '\xC8\xF6\xB4\xF3\xCB\xD5...' for column 'txt' at row 1---字符串问题
今天做大数据用一个字段类型为text 的字段,然后传输,一直 出现这个错误;
之后一直百度,改编码:
从utf-8 改到 gbk 又该回去 解决不了
最后。。。。。。。
原来是我本地的那个txt 文件保存的是ANISI 的格式 然后另存为一下,存成utf-8的格式传输成功;
代码附上:
工具类就不写了,自己写就行,用的是cp30连接池!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.rowset.serial.SerialBlob;
import org.apache.commons.io.IOUtils;
public class BlobDemo {
public static void main(String[] args) {
//test1(new File("D:/a.txt"));
test2("小说");
//savePicture(new File("D:/d.jpg"));
//readPicture("图片", new File("D:/copyd.jpg"));
}
//存储大文本
public static void test1(File file){
Connection conn =null;
PreparedStatement pstm =null;
try {
conn = Demo.getConnection1();
String sql ="insert into txt1 values(?,?)";
pstm = conn.prepareStatement(sql);
pstm.setString(1, "小说");
InputStream in = new FileInputStream(file);
byte [] b =IOUtils.toByteArray(in);
Blob b2 = new SerialBlob(b);
pstm.setBlob(2, b2);
int x=pstm.executeUpdate();
System.out.println(1);
if(x>0){
System.out.println("插入小说成功");
}else{
System.out.println("插入小说失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//读取文本
public static void test2(String txtName){
Connection conn =null;
PreparedStatement pstm = null;
try {
conn = Demo.getConnection();
String sql ="select * from txt1 where name=?";
pstm = conn.prepareStatement(sql);
pstm.setString(1, txtName);
ResultSet rs = pstm.executeQuery();
if(rs.next()){
//首先获取文本内容,通过Clob来接
Clob clob = rs.getClob("txt");
//之后通过clop对象的subString方法一个字段一个字段来读取
String text =clob.getSubString(1,(int) clob.length());
//输出文本内容
System.out.println("读取成功,小说内容为"+text);
clob.free();
}
} catch (Exception e) {
System.out.println("读取失败");
}
}
//存储图片
public static void savePicture(File file){
Connection conn =null;
PreparedStatement pstm = null;
try {
conn = Demo.getConnection1();
String sql ="insert into picture values(?,?)";
pstm = conn.prepareStatement(sql);
pstm.setString(1, "图片");
InputStream in = new FileInputStream(file);
byte [] b =IOUtils.toByteArray(in);
Blob b1 = new SerialBlob(b);
pstm.setBlob(2, b1);
int x=pstm.executeUpdate();
if(x>0){
System.out.println("插入图片成功");
}else{
System.out.println("插入图片失败");
}
} catch (Exception e) {
e.printStackTrace();
}
}
//读取图片
public static void readPicture(String name,File file){
Connection conn =null;
PreparedStatement pstm = null;
try {
//-----------------------------------
conn = Demo.getConnection();
String sql ="select p from picture where name=?";
pstm = conn.prepareStatement(sql);
//----------------------------------------
pstm.setString(1, name);
ResultSet rs = pstm.executeQuery();
if(rs.next()){
//---------------------------------------------
//首先获取文本内容,通过获取字段名,得到流
Blob ib = rs.getBlob("p");
InputStream in = ib.getBinaryStream();
OutputStream out =new FileOutputStream(file);
IOUtils.copy(in, out);
//--------------------------------------------------------------
System.out.println("读取成功,保存路径为:"+file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("读取失败");
}
}
}