0% found this document useful (0 votes)
11 views1 page

Insert Picture Into DB

This document shows how to insert a picture into a MySQL database table using Java. It defines a table called MyPictures with columns for an ID, name, and photo. It then shows Java code that loads the MySQL driver, connects to a database, prepares an INSERT statement, sets the column values including reading a photo file into a binary stream, executes the statement, and commits the transaction.

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Insert Picture Into DB

This document shows how to insert a picture into a MySQL database table using Java. It defines a table called MyPictures with columns for an ID, name, and photo. It then shows Java code that loads the MySQL driver, connects to a database, prepares an INSERT statement, sets the column values including reading a photo file into a binary stream, executes the statement, and commits the transaction.

Uploaded by

govindbirajdar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

/

Defining the Table: Jracle and MySql



create table MyPictures (
id INT PRIMARY KEY,
name VARCHAR(0),
photo BLJB
);
/
import java.io.File;
import java.io.FileInputStream;
import java.io.IJException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertPictureToMySql ,
public static void main(String, args) throws Exception, IJException, SQLEx
ception ,
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/dat
abaseName", "root", "root");
String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (
., ., .)";

FileInputStream fis = null;
PreparedStatement ps = null;
try ,
conn.setAutoCommit(false);
File file = new File("myPhoto.png");
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setString(1, "001");
ps.setString(2, "name");
ps.setBinaryStream(3, fis, (int) file.length());
ps.executeUpdate();
conn.commit();
, finally ,
ps.close();
fis.close();
,
,
,

You might also like