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)