Blob

想把图片存到数据库中,图片是二进制文件,我觉得用hibernate比较好,然后就开始写,结果一直出这个问题

Caused by: 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 '?疟5拧3${xv*??zQ?韓跰?sg<?沬g=\\M呀鱉[a?癢Z\'k_}蒜!軌?贯蘟D' at line 1

我并没有写sql代码,还一直提示我报错。

先理一下思路:

1:Student类以及对象关系映射文件

package com.zhao.entity;

import java.io.Serializable;

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private byte[] picture;

    public Student() {
        // TODO Auto-generated constructor stub
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public byte[] getPicture() {
        return picture;
    }
    public void setPicture(byte[] picture) {
        this.picture = picture;
    }


}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-5-21 20:09:22 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.zhao.entity.Student" table="STUDENT">
        <id name="id" type="int">
            <column name="stu_id" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="stu_name" />
        </property>
        <property name="picture" type="binary">
            <column name="stu_picture" />
        </property>
    </class>
</hibernate-mapping>

2:hibernate配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">807824</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/zhao</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="com/zhao/entity/Student.hbm.xml" />
    </session-factory>
</hibernate-configuration>

3:

其实就这么简单,但是我开始的时候用的是Blob,后来才用byte[]。

用Blob就一直出错,也查找了一些资料,hibernate3和hibernate4对blob的不同处理也都尝试过,但是session 保存的时候还是出错,错误就是不符合语法。

所以我决定大改一遍

在Student类中用byte[]  Student.hbm.xml中用binary  MySQL中用blob.  这样就不出问题了。

create table student(
    stu_id int primary key not null auto_increment,
    stu_name varchar(20),
    stu_picture blob
);

4:

插入数据

package com.zhao.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import com.zhao.entity.Student;

public class Main {

    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
                .buildServiceRegistry();
        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
        Session session = factory.openSession();
        Transaction transaction = session.beginTransaction();

        Student student = new Student();
        // 获取照片文件
        File file = new File("E:\\xue.jpg");
        if (!file.exists()) {
            throw new Exception("找不到图片");
        }
        @SuppressWarnings("resource")
        InputStream inputStream = new FileInputStream(file);
        byte[] bs = new byte[inputStream.available()];
        student.setName("root");
        student.setPicture(bs);
        session.save(student);
        
        transaction.commit();
        session.close();
        factory.close();
    }

}

查看数据库

好吧,问题解决了。近期再研究研究Blob 问题出在哪了吧。

posted @ 2016-05-21 22:59  假寐的我  阅读(237)  评论(1编辑  收藏  举报