Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- public class MainWrite
- {
- public static void main(String[] args)
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/training", "root", "13751010");
- connection.setAutoCommit(false);
- String sql = "INSERT INTO training.FILES values (?, ?)";
- PreparedStatement preparedStatement = connection.prepareStatement(sql);
- File file = new File("/home/mohamed/Desktop/document.pdf");
- FileInputStream fileInputStream = new FileInputStream(file);
- preparedStatement.setString(1, file.getName());
- preparedStatement.setBinaryStream(2, fileInputStream, file.length());
- preparedStatement.executeUpdate();
- connection.commit();
- fileInputStream.close();
- preparedStatement.close();
- connection.close();
- }
- catch (SQLException | IOException | ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement