Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MainRead
- {
- public static void main(String[] args)
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/training", "root", "13751010");
- String sql = "SELECT * FROM training.FILES";
- PreparedStatement preparedStatement = connection
- .prepareStatement(sql);
- ResultSet resultSet = preparedStatement.executeQuery();
- while (resultSet.next())
- {
- String fileName = resultSet.getString(1);
- InputStream inputFileStream = resultSet.getBinaryStream(2);
- FileOutputStream outputFileStream = new FileOutputStream(
- "/home/mohamed/Desktop/Data/" + fileName);
- byte[] buffer = new byte[1];
- while (inputFileStream.read(buffer) > 0)
- {
- outputFileStream.write(buffer);
- }
- inputFileStream.close();
- outputFileStream.close();
- }
- preparedStatement.close();
- connection.close();
- }
- catch (SQLException | ClassNotFoundException | IOException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement