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/
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.
– Guilherme Nascimento
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 aJTable
? 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.– Renan Gomes
Saving the file in the database is not a good way, as far as I know.
– Guilherme Nascimento
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).
– Bacco