Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9.  
  10. public class MainRead
  11. {
  12.     public static void main(String[] args)
  13.     {
  14.         try
  15.         {
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.             Connection connection = DriverManager.getConnection(
  18.                     "jdbc:mysql://localhost:3306/training", "root", "13751010");
  19.  
  20.             String sql = "SELECT * FROM training.FILES";
  21.             PreparedStatement preparedStatement = connection
  22.                     .prepareStatement(sql);
  23.             ResultSet resultSet = preparedStatement.executeQuery();
  24.  
  25.             while (resultSet.next())
  26.             {
  27.                 String fileName = resultSet.getString(1);
  28.  
  29.                 InputStream inputFileStream = resultSet.getBinaryStream(2);
  30.                 FileOutputStream outputFileStream = new FileOutputStream(
  31.                         "/home/mohamed/Desktop/Data/" + fileName);
  32.  
  33.                 byte[] buffer = new byte[1];
  34.                 while (inputFileStream.read(buffer) > 0)
  35.                 {
  36.                     outputFileStream.write(buffer);
  37.                 }
  38.                 inputFileStream.close();
  39.                 outputFileStream.close();
  40.             }
  41.             preparedStatement.close();
  42.             connection.close();
  43.         }
  44.         catch (SQLException | ClassNotFoundException | IOException e)
  45.         {
  46.             e.printStackTrace();
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement