62,615
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `photo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`picture` blob,
PRIMARY KEY (`id`)
)
public void save(){
try{
String sql="insert into photo(picture) values(?)";
preSt=conn.prepareStatement(sql);//preSt为PreparedStatement对象
File file=new File("a.jpg");
fileInputStream=new FileInputStream(file);
preSt.setBinaryStream(1,fileInputStream);
preSt.executeUpdate();
fileInputStream.close();
}catch(Exception e){
e.printStackTrace();
}
closePps();//关闭preSt
DataBaseUtil.closeConnection();//关闭数据库的链接
}
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 'qb?tyò?s??+?{??°?m?”?‘—???mmZ.>?????QM?X?·?\"?’k5eáA??geù?F·?%^;—r’?S&?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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 cn.yang.jdbc.CRUD.save(CRUD.java:43)
at cn.yang.jdbc.CRUD.main(CRUD.java:32)
public void save(){
try{
File file=new File("01.gif");
fileInputStream=new BufferedInputStream(new FileInputStream(file));
byte[] bytes=new byte[(int)file.length()];
fileInputStream.read(bytes);//得到图片的字节数组
preSt=getPps("insert into photo(picture) values(?)");
preSt.setBytes(1,bytes);
preSt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}
closePps();
try{
fileInputStream.close();
}catch(Exception e){
e.printStackTrace();
}
DataBaseUtil.closeConnection();
}
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 '???1kZ\\F5?X?(*??l?\0w÷§ó?En^`???r?”c—?
*z{?“$ú|?5??G2]?R5N7?lf#N????' at line 1
package test.mysql;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestBlob {
public static void insBlob(String fileName) {
Connection con = null;
PreparedStatement ps = null;
BufferedInputStream bis = null;
try {
con = MConnection.getConn();
String sql = "insert into testblob(image) values(?)";
ps = con.prepareStatement(sql);
File file = new File(fileName);
bis = new BufferedInputStream(new FileInputStream(file));
ps.setBinaryStream(1, bis, new Long(file.length()).intValue());
int ret = ps.executeUpdate();
System.out.println("insert file finished:" + ret);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void updBlob(int id, String fileName) {
Connection con = null;
PreparedStatement ps = null;
BufferedInputStream bis = null;
try {
con = MConnection.getConn();
String sql = "update testblob set image=? where id=?";
ps = con.prepareStatement(sql);
File file = new File(fileName);
bis = new BufferedInputStream(new FileInputStream(file));
ps.setBinaryStream(1, bis, new Long(file.length()).intValue());
ps.setInt(2, id);
int ret = ps.executeUpdate();
System.out.println("update file finished:" + ret);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void selBlob(int id, String saveFileName) {
InputStream is = null;
OutputStream os = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = MConnection.getConn();
String sql = "select image from testblob where id = ?";
ps = con.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
is = rs.getBinaryStream(1);
os = new FileOutputStream(saveFileName);
byte[] bytes = new byte[1024];
int read = 0;
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
}
System.out.println("get file finished!");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String fileName = "f:/notebook00.gif";
TestBlob.insBlob(fileName);
String saveFileName = "f:/notebook01.bak.gif";
TestBlob.selBlob(1, saveFileName);
TestBlob.updBlob(1, fileName);
}
}