
How can I insert images into a Mysql database?

Hi friends
With the help of this example, you can insert image in data base...
import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement;
public class InsertImageInMySql {
public static void main(String[] args) {
try {
int result = 0;
String mysqlDriver = "com.mysql.jdbc.Driver";
Class.forName(mysqlDriver).newInstance();
String connectionString = "jdbc:mysql://192.168.10.13:3306/studentinformation";
String username = "root";
String password = "root";
Connection con = DriverManager.getConnection(connectionString,
username, password);
PreparedStatement prestmt = null;
// Create a binary stream
File imageFile = new File("lotus_temple.jpg");
FileInputStream fis = new FileInputStream(imageFile);
String insertImage = "insert into picture values(?,?)";
prestmt = con.prepareStatement(insertImage);
prestmt.setInt(1, 7);
prestmt.setBinaryStream(2, fis, fis.available());
result = prestmt.executeUpdate();
if (result == 1) {
System.out.println("Image Successfully Inserted in database!");
} else {
System.out.println("Image not Inserted in database!");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Thanks.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.