how to insert an image in to mysql database using java program 3f

Solutions on MaxInterview for how to insert an image in to mysql database using java program 3f by the best coders in the world

showing results for - "how to insert an image in to mysql database using java program 3f"
Beverly
09 Feb 2020
1import java.io.FileInputStream;
2import java.io.InputStream;
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6public class InsertImageToMySqlDB {
7   public static void main(String args[]) throws Exception{
8      //Registering the Driver
9      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
10      //Getting the connection
11      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
12      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
13      System.out.println("Connection established......");
14      PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?,?)");
15      pstmt.setString(1, "sample image");
16      //Inserting Blob type
17      InputStream in = new FileInputStream("E:\\images\\cat.jpg");
18      pstmt.setBlob(2, in);
19      //Executing the statement
20      pstmt.execute();
21      System.out.println("Record inserted......");
22   }
23}