public static void write(File file) throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/abs","root",""); String sql="insert into file (id,file) values (?,?)"; PreparedStatement pst=co.prepareStatement(sql); pst.setInt(1, 1); InputStream in=new FileInputStream(file); pst.setBinaryStream(2, in, in.available()); pst.executeUpdate(); System.out.println("写入Ok!"); in.close(); pst.close(); co.close(); } public static void main(String[] args) throws Exception{ File file=new File("E://a.jpg"); write(file); }
运行提示sql语句错误???
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 '?kK ?????—!?Vé??è???é????????b???} ???????k??±?a6????#é????Yé??'?è??Cé???' at line 1
SQL语句错误,你先把sql语句打印出来,然后再找错。
看看你mysql支持不支持图片流的操作,
把数据库的编码方式改成utf8就OK了