How to store a pdf file in a Mysql database?

Asked

Viewed 12,040 times

0

I am developing a project and one of my tables in Mysql will contain files .pdf. The table 'arquivopdf':

+ - - - - - - - - - - - - - - - - - - - - +
|    numerocotacao     |     arquivopdf   |
+ - - - - - - - - - - - - - - - - - - - - +

For each quotation number I will have several files .pdf and I have several quotation numbers.

I wonder how I can include these files .pdf in the Mysql table using JFrame. How can I pick them up from the Mysql table and play them on a JTable to select a pdf and open it.

I’m sorry if I wasn’t clear, but what I wanted was instead to include the path in the database was to write the file there in the database.

But if there’s no way, how do I include the path in the database by writing it to a folder on the server and then opening it.

  • generally only include the file’s PATH in the database, the file is saved in the server directory. At least I see this as the best way.

  • 1

    You want to fetch the information in Mysql and insert it into a JTable? You want to fetch the file .pdf, read the content and play that content on a JTable? Wants to save the path file? Want to know how to save the file in a certain path? You need to have a focus on your question and not create multiple questions within and a single one, this makes it too wide.

  • Saving the file in the database is not a good way, as far as I know.

  • Jadir, the problem I see in your question is the same one @Renan commented on. It’s separate problems to take the UI data and write it to DB. It would be the case that you explain better what you want to happen, instead of trying to explain just how you tried to do it. The ideal would be to add details of where these files are (local HD, internet, etc.) and how the user will add them, for example (whether to be choosing from a list, whether to be sending the file, whether to select an existing file, etc).

2 answers

1

Dude, a viable alternative is to use a directory to store the PDF s and save in the flock their path, then you mount a function that searches the path of such a file with such name, if it is with such a select file name in the path field, and uses the string with the path to open the file.

0

To save a file to a bank do as follows:

public boolean insertFile( File f ){
    Connection c = this.getConnection();//busca uma conexao com o banco
    try {
        PreparedStatement ps = c.prepareStatement("INSERT INTO arquivo( id, nome, arquivo ) VALUES ( nextval('seq_arquivo'), ?, ? )");

        //converte o objeto file em array de bytes
        InputStream is = new FileInputStream( f );
        byte[] bytes = new byte[(int)f.length() ];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        ps.setString( 1, f.getName() );
        ps.setBytes( 2, bytes );
        ps.execute();
        ps.close();
        c.close();
        return true;

    } catch (SQLException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return false;
}

Understanding the method: First, we convert the file to an array of bytes (byte []). Then we use the Preparedstatement setBytes to store the file in the specified column.

That’s it: by using this method, we keep the name and the file itself in a table in the database.

To recover the file do the following:

public File getFile( int id ){
    Connection c = this.getConnection();//busca uma conexao com o banco
    File f = null;
    try {
        PreparedStatement ps = c.prepareStatement("SELECT id, nome, arquivo FROM arquivo WHERE id = ?");
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        if ( rs.next() ){
            byte [] bytes = rs.getBytes("arquivo");
            String nome = rs.getString("nome");

            //converte o array de bytes em file
            f = new File( "/local_a_ser_salvo/" + nome );
            FileOutputStream fos = new FileOutputStream( f);
            fos.write( bytes );
            fos.close();
        }
        rs.close();
        ps.close();
        c.close();
        return f;
} catch (SQLException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
return null;
}

The procedure is the reverse: returns the column value in array of bytes and creates a disk file from these bytes.

If you don’t know how to create an object of the type File take a look at the Java documentation that is easy to understand.

Source: http://blog.hallanmedeiros.com/2011/11/16/salvar-arquivos-em-banco-de-dados-com-java/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.