Skip to main content
deleted 114 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k
Source Link
Vicky
  • 9.6k
  • 17
  • 74
  • 91

Fibd details on this http://www.technicalkeeda.com/java-tutorial/java-jdbc/how-to-write-image-into-mysql-database-using-java

package com.technicalkeeda; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class InsertImageTest { /** * This is used to get the Connection * * @return */ public Connection getConnection() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/technicalkeeda", "root", ""); } catch (Exception e) { System.out.println("Error Occured While Getting the Connection: - " + e); } return connection; } /** * Insert Image */ public void insertImage() { Connection connection = null; PreparedStatement statement = null; FileInputStream inputStream = null; try { File image = new File("C:/honda.jpg"); inputStream = new FileInputStream(image); connection = getConnection(); statement = connection .prepareStatement("insert into trn_imgs(img_title, img_data) " + "values(?,?)"); statement.setString(1, "Honda Car"); statement.setBinaryStream(2, (InputStream) inputStream, (int) (image.length())); statement.executeUpdate(); } catch (FileNotFoundException e) { System.out.println("FileNotFoundException: - " + e); } catch (SQLException e) { System.out.println("SQLException: - " + e); } finally { try { connection.close(); statement.close(); } catch (SQLException e) { System.out.println("SQLException Finally: - " + e); } } } /*** * Execute Program * * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { InsertImageTest imageTest = new InsertImageTest(); imageTest.insertImage(); } }