本帖最后由 何俊森 于 2013-4-26 10:14 编辑
- package org.klaus.lobdemo;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- public class ClobDemo01 {
- /**
- * @param args
- */
- //定义mysql 的数据库驱动程序
- public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
- public static final String DBURL = "jdbc:mysql://localhost:3306/mldn";
- public static final String DBUSER = "root";
- public static final String DBPASS = "mysqladmin";
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
-
- Connection conn = null; //数据库连接
- PreparedStatement pstmt = null; //数据库操作
- String name = "jacob";
- String sql = "insert into userclob(name,note)values(?,?)";
- Class.forName(DBDRIVER); //加载驱动程序
- conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); //连上mysql数据库
- pstmt = conn.prepareStatement(sql); //实例化{reparedStatement
- File f = new File("d:" + File.separator + "feiq.txt"); //声明一个File对象,用于要操作的大文本文件
- InputStream input = new FileInputStream(f); //输入流读取文件
- //设置"?"内容
- pstmt.setString(1, name);
- pstmt.setAsciiStream(2,input,(int)f.length());
- pstmt.executeUpdate(); //执行数据库更新操作
- pstmt.close(); //操作关闭
- conn.close(); //连接关闭
- }
- }
复制代码 文本feiq .txt有中文内容就会抛异常:Exception in thread "main" java.sql.SQLException: Incorrect string value: '\xBF\xC9\xD3\xD0\xBF\xC9...' for column 'note' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
at org.klaus.lobdemo.ClobDemo01.main(ClobDemo01.java:32)
其中我的mysql中的character_set是这样的,一种是敲用户名进入的,另一个是安装了mysql后,开始目录下就有,直接点,不用输入用户名,要输入密码进去的,为什么显示字符设置不一样。我的 mysql是mysql-installer-community-5.5.28.3.msi。
|
|