//String sql = "insert into test_blob(id,content) values(?,?)";
String sql = "insert into test_blob(id,content) values(?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, UUID.randomUUID().toString());
// 类加载器加载资源
URL uri = BlobTest.class.getClassLoader().getResource("cn/zengfansheng/blob/money.jpg");
File file = new File(URLDecoder.decode(uri.getPath(), "utf-8"));
InputStream is = new FileInputStream(file);
pstmt.setBinaryStream(2, is, (int)file.length());
// 执行sql语句
int i = pstmt.executeUpdate();
System.out.println(i>0?"数据写入数据库成功":"数据写入数据库失败");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally{
// 关闭资源,从里到外
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
复制代码
为什么会报这个错误,前面我存Clob时候代码也差不多,但是没有报错。
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 '????—??”????è????????é??é??Rè??U???[????—?é…??‰?.q???V??°8RMFè¤?????è?????p
7' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at cn.zengfansheng.blob.BlobTest.write(BlobTest.java:50)
at cn.zengfansheng.blob.BlobTest.main(BlobTest.java:20)
复制代码
作者: ㄨ____陌生 时间: 2013-4-5 13:22
出错了要看报错的信息,根据这个去修改代码。在SQL 语句里有问题。应该不是数据库连接的问题。改下你的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 作者: 冯超 时间: 2013-4-5 16:23
兄弟看看·我写的存入与读取
public static void creat2() throws SQLException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream in = null;
try {
//2.连接
conn = (Connection) JdbcUtils.getConnection();
//3.创建语句
File file = new File("f:\\qq.jpg");
in = new BufferedInputStream(new FileInputStream(file));
String sql = "insert into blob_test(blob_tes) values(?)";
ps = conn.prepareStatement(sql);
ps.setBinaryStream(1, in, (int)file.length());
//4.执行
ps.executeUpdate();
//5.处理结果
System.out.println("插入成功!");
} finally {
in.close();
JdbcUtils.free(rs, ps, conn);
}
}
public static void read2() throws SQLException, IOException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
InputStream in = null;
try {
//2.连接
conn = (Connection) JdbcUtils.getConnection();
//3.创建语句
String sql = "select blob_tes from blob_test";
ps = conn.prepareStatement(sql);
//4.执行语句
rs = ps.executeQuery();
//5.处理
FileOutputStream out = new FileOutputStream(new File("F:\\123.jpg"));
while(rs.next()) {
//Clob clob = rs.getClob(1);
InputStream ins = rs.getBinaryStream(1); //in --->read