java setBinaryStream error for mysql

来源:互联网 发布:java编写计算器教程 编辑:程序博客网 时间:2024/05/22 14:34


     Class.forName(driver);
     Connection conn=DriverManager.getConnection(url, user, password);
     String sql="insert into [test] ([name],[img])values(?,?)";
     InputStream is=new FileInputStream(new File("src/lsnu/lgf/saveimage/haha.jpg"));
     System.out.println("len="+is.available());
     
     PreparedStatement ps=conn.prepareStatement(sql);
     ps.setString(1, "name");
     ps.setBinaryStream(2, is,is.available());

     System.out.print(ps.execute());


presentation:Exception in thread "main" java.sql.SQLException: 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 '[test] ([name],[img])values('ggggg',_binary'ertwertwederywgadfhsdfhgf

change:
      String sql="insert into test (name,img)values(?,?)";

is ok;