首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

MySQL插入图片有关问题

2012-12-21 
MySQL插入图片问题package com.wow.lobimport java.io.Fileimport java.io.FileInputStreamimport java

MySQL插入图片问题


package com.wow.lob;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.junit.Test;

import com.wow.util.JDBCUtil;

/*
  这个是创建的表
  create table test2
  (
  id int primary key auto_increment,
  image longblob
  );
 */
public class Demo2 {
@Test
public static void insert() throws Exception{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = JDBCUtil.getConection();
String sql = "insert into test2(image) values(?)";
ps = conn.prepareStatement(sql);
URL url = Demo2.class.getClassLoader().getResource("1.jpg");
File file = new File(url.getPath());
ps.setBinaryStream(1, new FileInputStream(file), file.length());
int num = ps.executeUpdate();
if(num>0){
System.out.println("插入成功");
}
}finally{
JDBCUtil.myClose(rs, ps, conn);
}
}


我想问下,为什么程序一执行到
int num = ps.executeUpdate();
  就出异常,说我sql语句有问题。二进制数据这么插入没错吧?求大神!!!
[最优解释]
你妹的,帮你找这个错误,害得耽误了我一个小时。记得把分都给我哈

帮你找到原因了,你的代码没有写错,错误的事你的mysql的数据库编码

下面是我的编码:


public class Test {

public static void main(String[] args) throws SQLException {

Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConection();
String sql = "insert into pic values(?,?,?)";
ps = conn.prepareStatement(sql);
File file = new File("c:/a.jpg");
FileInputStream fis = new FileInputStream("c:/a.jpg");
ps.setInt(1, 1);
ps.setString(2, "a.jpg");
ps.setBinaryStream(3, fis, (int)file.length());
int num = ps.executeUpdate();
if (num > 0) {
System.out.println("插入成功");
}else{
System.err.println("插入失败");
}
}catch (Exception e) {
e.printStackTrace();
} finally {[code=Java]

JDBCUtil.myClose(rs, ps, conn);
}
}
}
[/code]

之前我的MySQL数据库是GBK,也是
int num = ps.executeUpdate();

报错如下:





用MySQL的配置工具MySQLInstanceConfig.exe  ,将MySQL数据库编码改成 : utf8后。

操作成功,截图如下:



[其他解释]
 你的逻辑代码执行的顺序错了
String sql = "insert into test2(image) values(?)";
            ps = conn.prepareStatement(sql);这里还没有获得你要插入的值呢
[其他解释]
刚刚看错了.......
你的sql报的是什么错,打印下你的sql语句,看看插进去的是什么,或者打印下你获得的图片路径。


[其他解释]
还有你的数据库的字段是int还是blob
 public void saveImage(String filePath){
  
   try {
    //读文件创建流
    File file=new File(filePath);
    long filesize=file.length();
    int filelength=(int)filesize;
    //blob数据类型用int,longblob用long,否则报错
    FileInputStream in=new FileInputStream(file);
    //存放流
    String sql="insert into tb_image(img) values(?)";
    PreparedStatement prst=conn.prepareStatement(sql);
    prst.setBinaryStream(1, in, filelength);

或prst.setBinaryStream(1,in,in.avaliable());    prst.executeUpdate();
   } catch (SQLException e1) {
    System.out.println("连接数据库有问题");
    e1.printStackTrace();
   } catch (IOException e) {
    System.out.println("IO有问题");
    e.printStackTrace();
   }
[其他解释]
URL url = Demo2.class.getClassLoader().getResource("1.jpg");
File file = new File(url.getPath());

=>
InputStream in = Demo2.class.getClassLoader().getResourceAsStream("1.jpg");
[其他解释]
如果你不会MySQL的数据库编码配置,我截图给你:




[其他解释]

引用:
如果你不会MySQL的数据库编码配置,我截图给你:

 character_set_client
    
[其他解释]

 character_set_connection
    

热点排行