Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.SQLException;
  8.  
  9. public class MainWrite
  10. {
  11.     public static void main(String[] args)
  12.     {
  13.         try
  14.         {
  15.             Class.forName("com.mysql.jdbc.Driver");
  16.            
  17.             Connection connection = DriverManager.getConnection(
  18.                     "jdbc:mysql://localhost:3306/training", "root", "13751010");
  19.             connection.setAutoCommit(false);
  20.            
  21.             String sql = "INSERT INTO training.FILES values (?, ?)";
  22.             PreparedStatement preparedStatement = connection.prepareStatement(sql);
  23.  
  24.             File file = new File("/home/mohamed/Desktop/document.pdf");
  25.             FileInputStream fileInputStream = new FileInputStream(file);
  26.            
  27.             preparedStatement.setString(1, file.getName());
  28.  
  29.             preparedStatement.setBinaryStream(2, fileInputStream, file.length());
  30.  
  31.             preparedStatement.executeUpdate();
  32.             connection.commit();
  33.  
  34.             fileInputStream.close();
  35.             preparedStatement.close();
  36.             connection.close();
  37.         }
  38.         catch (SQLException | IOException | ClassNotFoundException e)
  39.         {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement