完全跟着教程做的,一字不差,就是出错,代码如下:static void create() throws SQLException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
String sql = "insert into blob_test(big_bit) values (?) ";
ps = conn.prepareStatement(sql);
File file = new File("c:/Bluehills.rar");
InputStream in = new BufferedInputStream(new FileInputStream(file)); ps.setBinaryStream(1, in, (int) file.length());
// 4.执行语句
int i = ps.executeUpdate();
in.close(); System.out.println("i=" + i);
} finally {
JdbcUtils.close(rs, ps, conn);
}
}错误信息如下:
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?U ?????
??&?|?V]??_?fC;???<????@???÷-?[?
??à<?x???úá??7p2?qù????¨ì<?' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1027)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2022)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1940)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1925)
at cn.itcast.jdbc.CopyOfBlobTest.create(CopyOfBlobTest.java:87)
at cn.itcast.jdbc.CopyOfBlobTest.main(CopyOfBlobTest.java:32)

解决方案 »

  1.   

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 把你的SQL语句用System.out.println();在后台打印出来,再复制到查询分析器(如果是sqlserver2000),看能不能执行,mysql也类似,自己完成看看,SQL语句绝对有问题的。
      

  2.   

    sql语句好像又没有错,又好像是编码有问题
      

  3.   

    在String sql="...",设置完参数后面加上输出,断点调试, 把sql拿到查询分析器里面执行,没问题在往下运行,这个工作做了你后面好多事会非常省心的
    ps.setBinaryStream(1, in, (int) file.length()[/color]);  看你这句话,
    而setBinaryStream(int parameterIndex,InputStream x);
    参数问题?我觉得如果是真样应该报编译错才对,你这是运行期异常那应该是sql的错误,把sql拿到查询分析器里面执行了再说吧
      

  4.   

    也可能是编码的问题.. 读文件的时候产生乱码... 先看sql sql没错再检查文件
      

  5.   

    编码问题,你去mysql安装目录下找到,my.ini,打开, 
    找到这个default-character-set=utf8, 
    下边应该是有2个这个属性的配置,一个是客户端的,一个是服务端, 
    你要是弄不清楚,就把2个全给修成utf8就成了 
    把编码设置成utf8,重新启动mysql服务, 
    重新建数据库就ok啦
      

  6.   

    sql语句没问题的,我如果是读文本文件就正常,读图片,rar都有问题
      

  7.   

    问题已解决,是编码问题,全部设置为utf8,然后重启数据库就ok了
    谢谢各位,明天结贴给分