我想把文件保存到MySql数据库,但总是报错,各位大侠帮忙看看,多谢!错误信息如下:
com.mysql.jdbc.exceptions.jdbc4.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 'ü??\0???\0~??\0??????÷????lDD*g???4{ò?`?áù!r’j0?V??w?b????????G???\0~?5??' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2409)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
at com.huigao.test.Test2.test(Test2.java:42)
at com.huigao.test.Test2.main(Test2.java:71)下面是我的测试代码:
public class Test2 {
private static final String URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String FILENAME = "126830711.jpg";
private static final String FILEPATH = "D:\\test\\126830711.jpg";

static{
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public static Connection getConn() throws Exception {
return DriverManager.getConnection(URL,USERNAME,PASSWORD);
}

public static void test(){
Connection conn = null;
PreparedStatement ps = null;
String sql = "insert into t_file(filename,content) values(?,?)";
try {
conn = getConn();
ps = conn.prepareStatement(sql);
ps.setString(1, FILENAME);
InputStream is = getFileStream();
ps.setBinaryStream(2, is, is.available());
ps.executeUpdate();
is.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(ps,conn);
}
}

private static void close(PreparedStatement ps, Connection conn) {
try {
if(ps != null)
ps.close();
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
} private static InputStream getFileStream() throws FileNotFoundException {
File file = new File(FILEPATH);
if(file.exists()){
return new FileInputStream(file);
}
return null;
} public static void main(String[] args) {
test();
}
}乍一看像是数据库编码问题,可是我改了数据库编码好像都不管用,utf8、gbk、gb2312都试过了。