Monday, August 30, 2010

JDBC -- Inserting binary data

Inserting Data :

    public void saveMedia(File file, short type) {
        FileInputStream io=null;
        try{
            io = new FileInputStream(file);
        }catch(IOException ioEx){
            ioEx.printStackTrace();
        }
        try {
            PreparedStatement statement = connection.prepareStatement("insert into media (content,mediatype,fileName) values(?,?,?)");
            statement.setBinaryStream(1, io, file.length());
            statement.setShort(2, type);
            statement.setString(3,file.getName());
            statement.executeUpdate();
            connection.close();
        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        } catch(ClassNotFoundException cnfEx){
            cnfEx.printStackTrace();
        }
    }

No comments:

Post a Comment