图像未使用Java存储在数据库中

时间:2018-12-09 14:52:09

标签: java mysql

我正在尝试使用BLOB数据类型将图像存储在数据库中,但是当我调用该类来插入它时,并没有按预期做,这很奇怪,因为它没有给我一个错误,所以我放了一个“ out.println()”对在方法调用之后,看它是否进入。

这是插入代码:

public class PatientFees
{
    [Key]               
    public int PatientID { get; set; }        
    public int EstimateAmount { get; set; }       
    public int Discount { get; set; }        
    public int AmountToPay { get; set; }

    public Patient Patient { get; set; }
    public List<PatientService> PatientService { get; set; }

}

public class PatientService
{             

    public int PatientID { get; set; }
    public PatientFees  PatientFees   { get; set; }
    public int ServiceID { get; set; }
    public Services  Services { get; set; }   

    public int Unit { get; set; }

}

public class Services
{
    [Key]
    public int ServiceID { get; set; }
    public string Procedure { get; set; }        
    public int Amount { get; set; }        
    public DateTime   TimeFrame { get; set; }

    public List<PatientService> PatientService { get; set; }
}

这是我的方法调用:

public void insertProfPic(int user_id, String file) {
        Connection conn = DatabaseConnection.getCon();
        FileInputStream imageInputStream = null;

        PreparedStatement prep = null;
        try{
            String query = "INSERT INTO images(user_id, images) VALUES (?,?)";
            prep = conn.prepareStatement(query);
            File fil = new File(file);
            imageInputStream = new FileInputStream(fil);

            prep.setInt(1, user_id);
            prep.setBinaryStream(2, imageInputStream,(int)file.length());

            prep.execute();
        }catch(FileNotFoundException | SQLException e){
            System.out.println(e);
        }finally {
                        // close resources 

            if(imageInputStream!=null){
                            try {
                                imageInputStream.close();
                            } catch (IOException ex) {
                                Logger.getLogger(imageUpload.class.getName()).log(Level.SEVERE, null, ex);
                            }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

尝试将图像编码为字节[]并将其存储为二进制数据