How to Insert Image in Database Using Java
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert an image in to MySQL database using Java program?
To hold an image in MySQL database generally blob type is used. Therefore, make sure that you have a table created with a blob datatype with the following description:
+-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | Name | varchar(255) | YES | | NULL | | | image | blob | YES | | NULL | | +-------+--------------+------+-----+---------+-------+
To insert an image in to MySQL database, follow the steps given below:
Step 1: Connect to the database
You can connect to a database using the getConnection() method of the DriverManager class.
Connect to the MySQL database by passing the MySQL URL which is jdbc:mysql://localhost/sampleDB (where sampleDB is the database name), username and password as parameters to the getConnection() method.
String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
Step 2: Create a Prepared statement
Create a PreparedStatement object using the prepareStatement() method of the Connection interface. To this method pass the insert query (with place holders) as a parameter.
PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?, ?)");
Step 3: Set values to the place holders
Set the values to the place holders using the setter methods of the PreparedStatement interface. Chose the methods according to the datatype of the column. For Example if the column is of VARCHAR type use setString() method and if it is of INT type you can use setInt() method.
And if it is of Blob type you can set value to it using the setBinaryStream() or setBlob() methods. To these methods pass an integer variable representing the parameter index and an object of InputStream class as parameters.
pstmt.setString(1, "sample image"); //Inserting Blob type InputStream in = new FileInputStream("E:\\images\\cat.jpg"); pstmt.setBlob(2, in);
Step 4: Execute the statement
Execute the above created PreparedStatement object using the execute() method of the PreparedStatement interface.
Example
import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertImageToMySqlDB { public static void main(String args[]) throws Exception{ //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?,?)"); pstmt.setString(1, "sample image"); //Inserting Blob type InputStream in = new FileInputStream("E:\\images\\cat.jpg"); pstmt.setBlob(2, in); //Executing the statement pstmt.execute(); System.out.println("Record inserted......"); } }
Output
Connection established...... Record inserted......
Published on 22-Mar-2019 10:47:35
- Related Questions & Answers
- How to insert an image in to Oracle database using Java program?
- How to insert DECIMAL into MySQL database?
- How to insert data into a MySQL database with Java?
- How to Insert an Image in HTML Page?
- How to insert Timestamp value in a database using JDBC program?
- How to insert images in Database using JDBC?
- Java application to insert null value into a MySQL database?
- How to get the id after INSERT into MySQL database using Python?
- Insert current date to the database in MySQL?
- How to insert an image in a Tkinter canvas item?
- How to insert a Python tuple in a MySQL database?
- How to flip an image using Java OpenCV library?
- How to rotate an image with OpenCV using Java?
- How to read an image using Java OpenCV library?
- How to write an image using Java OpenCV library?
How to Insert Image in Database Using Java
Source: https://www.tutorialspoint.com/how-to-insert-an-image-in-to-mysql-database-using-java-program