使用SQL和Java上载映像

时间:2012-12-11 18:02:05

标签: java sql oracle dao

我坚持尝试使用我的Java代码上传图像。我正在使用数据访问对象并具有ojdbc5和ordim库。 我遇到的主要问题是尝试使用以下代码行插入图像:stmtInsert.setImage(8,ORDSYS.ORDImage.init());

我已经坚持了很久。

public void createImageInfo(           
                                     final String user_ID,
                                     final int photo_ID,
                                     final int num_views,
                                     final double price,
                                     final String photo_Name,
                                     final String imageCategory,
                                     final Calendar dateUploaded,
                                     final ORDSYS.ORDImage.init(image))
{


    PhotoSysDAO result = null;
    PreparedStatement stmtInsert = null;

    Connection conn = null;
    try
    {
    conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@oracle server",
                    "username", "password"); 
                    //open a connection to db...needs your account and password

        //int ratingID = RatingsDAO.getUniqueRatingId(conn);

        StringBuilder sbInsert = new StringBuilder();


        sbInsert.append("INSERT INTO ");
        sbInsert.append("Photos");
        sbInsert.append(" VALUES (");

        sbInsert.append("SequenceSet.seq_photo_id.nextval, ?, ?, ?, ?, ?, ?, ?, ?)");
                    Calendar date =  Calendar.getInstance();

                    stmtInsert.setInt(1, photo_ID);
        stmtInsert.setString(2, user_ID);
        stmtInsert.setInt(3, num_views);
                    stmtInsert.setDouble(4, price);
                    stmtInsert.setString(5, photo_Name);
                    stmtInsert.setString(6, imageCategory);
                    stmtInsert.setString(7, new SimpleDateFormat("dd/MMM/yyyy").format(date));
                    //stmtInsert.setImage(8, photo.ORDSYS.ORDIMGB);
                    stmtInsert.setImage(8, ORDSYS.ORDImage.init()); 
        int rows = stmtInsert.executeUpdate();

        if (rows != 1)
        {
            throw new SQLException("executeUpdate return value: " + rows);
        }

    }
    catch (SQLException ex)
    {   
        System.out.println("There was an error in connecting to the database");
    }
    finally
    {
        PhotoSysDAO.closeStatement(stmtInsert);
        PhotoSysDAO.closeJDBCConnection(conn);
    }
    //return result;
}

1 个答案:

答案 0 :(得分:-2)

我建议您,不要将图像存储在数据库中,否则会损害您的数据库存储空间。性能。 将照片存储在文件夹位置&将路径存储在数据库中。