这是我的数据库建表代码:
CREATE TABLE userclob(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
note LONGTEXT
) ;JAVA code :import java.sql.Connection ;
import java.sql.DriverManager ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
import java.sql.PreparedStatement ;public class ClobDemo{
//MySQL-JDBC驱动
public static final String DBRIVER = "org.gjt.mm.mysql.Driver" ;
//MySQL数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
//MySQL 用户名
public static final String DBUSER = "root" ;
//MySQL 密码
public static final String DBPASSWORD = "mysqladmin" ;
public static void main(String args[]) throws Exception{
Connection conn = null ;
PreparedStatement prs = null ;
String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;
Class.forName(DBRIVER) ; //加载JDBC驱动
//实例化Connection对象获得与数据库连接
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
//实例化PreparedStatement对象
prs = conn.prepareStatement(sql) ; //使用预处理写入SQL语句
String name = "罗星笔记";
//用输入流读取文件
File f = new File("d:" + File.separator + "sql.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
prs.setString(1,name) ;
prs.setAsciiStream(2,input) ;
prs.executeUpdate() ; //执行更新操作
input.close() ; //关闭输入流
conn.close() ; //关闭数据库
}
}抛错:D:\test\clobdemo>java ClobDemo
Exception in thread "main" java.sql.SQLException: Incorrect string value: '\xB4\
xB4\xBD\xA8\xBA\xCD...' for column 'note' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja
va:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:2318)
at ClobDemo.main(ClobDemo.java:33)
CREATE TABLE userclob(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
note LONGTEXT
) ;JAVA code :import java.sql.Connection ;
import java.sql.DriverManager ;
import java.io.File ;
import java.io.FileInputStream ;
import java.io.InputStream ;
import java.sql.PreparedStatement ;public class ClobDemo{
//MySQL-JDBC驱动
public static final String DBRIVER = "org.gjt.mm.mysql.Driver" ;
//MySQL数据库连接地址
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ;
//MySQL 用户名
public static final String DBUSER = "root" ;
//MySQL 密码
public static final String DBPASSWORD = "mysqladmin" ;
public static void main(String args[]) throws Exception{
Connection conn = null ;
PreparedStatement prs = null ;
String sql = "INSERT INTO userclob(name,note) VALUES (?,?) " ;
Class.forName(DBRIVER) ; //加载JDBC驱动
//实例化Connection对象获得与数据库连接
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
//实例化PreparedStatement对象
prs = conn.prepareStatement(sql) ; //使用预处理写入SQL语句
String name = "罗星笔记";
//用输入流读取文件
File f = new File("d:" + File.separator + "sql.txt") ;
InputStream input = null ;
input = new FileInputStream(f) ;
prs.setString(1,name) ;
prs.setAsciiStream(2,input) ;
prs.executeUpdate() ; //执行更新操作
input.close() ; //关闭输入流
conn.close() ; //关闭数据库
}
}抛错:D:\test\clobdemo>java ClobDemo
Exception in thread "main" java.sql.SQLException: Incorrect string value: '\xB4\
xB4\xBD\xA8\xBA\xCD...' for column 'note' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.ja
va:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java
:2318)
at ClobDemo.main(ClobDemo.java:33)
void setAsciiStream(int parameterIndex,
InputStream x,
int length)
throws SQLException
文件是二进制,longtext是长文本
这个方法有很多重载:有两参的
void setAsciiStream(int parameterIndex,
InputStream x)
throws SQLException
这说明你建表的类型有错哦!你自己再查下看。
看看setAsciiStream把InputStream 如何转化了才是关键。
你好,这是mldn李兴华用的方法 直接把输入流参数放在setAsciiStream()里就行了 , 这是数据表的结构:
mysql> DESC userclob ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| note | longtext | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
我的纯本地驱动*.jar 中Driver.class驱动文件 包路径就是:"org.gjt.mm.mysql.Driver"
你将文本改成abcdealsjd;als,试试
如果成了,说明编码不统一引起异常
包括使用 javac -encoding ..... 再试试
我的数据库编码格式是gbk的 你看看是不是这里的问题;
我的数据库编码格式是gbk的 你看看是不是这里的问题;
你把要加载的文本文件的内容全改成英文字符试试,如果还不成再说
就是编码的问题,把文本文件里面的内容改成英文的就OK了
但是又要这样修改成gbk编码来输入中文内容呢?
javac -encodeing GBK xx.java
Reader InputStream
├BufferedReader BufferedInputStream
│ └LineNumberReader LineNumberReader
├CharArrayReader ByteArrayInputStream
├InputStreamReader (none)
│ └FileReader FileInputStream
├FilterReader FilterInputStream
│ └PushbackReader PushbackInputStream
├PipedReader PipedInputStream
└StringReader StringBufferInputStream
Reader reader,
int length)
throws SQLException
将给定参数设置为给定 Reader 对象,后者是给定的字符数长度。在将一个非常大的 UNICODE 值输入到 LONGVARCHAR 参数时,通过 java.io.Reader 对象发送它可能更实际一些。将根据需要从流中读取数据,一直读取到文件末尾。JDBC 驱动程序将执行从 UNICODE 到数据库 char 格式的任何必要转换。
注:此流对象既可以是一个标准 Java 流对象,也可以是实现标准接口的用户自己的子类。
参数:
parameterIndex - 第一个参数是 1,第二个参数是 2,依此类推。
reader - 包含 Unicode 数据的 java.io.Reader 对象
length - 流中的字符数
抛出:
SQLException - 如果发生数据库访问错误
show full columns from userclob;
如果不是GBK,可用下面方法修改
alter table userclob modify note varchar(30) character set gbk;