Java – Good example for upload file to Oracle database with GUI in Java

databasefilejavaoracleswing

I am looking for a good example of upload & download file to/from Oracle database in Java to get idea. Would you please help me if any of you know a good example?

Best Solution

Here for upload:

public class FileToDatabase {
    public static void main(String[] args) throws Exception {
            String fileName = "C:/input.txt";
            File file = new File(fileName);
            FileInputStream fis = new FileInputStream(file);
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/test", "root", "root");
            PreparedStatement pstmt = conn
                            .prepareStatement("insert into file( file, file_data) values ( ?, ?)");
            pstmt.setString(1, file.getName());
            pstmt.setBinaryStream(2, fis, (int) file.length());
            pstmt.executeUpdate();
    }

Here for download

I found a good piece of code for file downloading from an application server, (in the link below). If you are going to use a web app, you could cache the file from the database into the application before the download.(Im interested in what other thinks about this alternative)

Link: http://www.daniweb.com/software-development/java/threads/154128