Hi Friend,
We are providing you a code that will insert multiple image in the database from frame.
import java.sql.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
public class InsertImage {
static JTextField text1=new JTextField(20);
static JTextField text2=new JTextField(20);
static JTextField text3=new JTextField(20);
static JButton button=new JButton("Save");
static JPanel panel=new JPanel(new GridLayout(4,2));
static JButton button1=new JButton("Browse");
static JButton button2=new JButton("Browse");
static JButton button3=new JButton("Browse");
static File file1,file2,file3;
public static void main(String[] args) {
panel.add(text1);
panel.add(button1);
panel.add(text2);
panel.add(button2);
panel.add(text3);
panel.add(button3);
panel.add(button);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file1 = chooser.getSelectedFile();
String name=file1.getName();
text1.setText(name);
}
}
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file2 = chooser.getSelectedFile();
String name=file2.getName();
text2.setText(name);
}
}
});
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file3 = chooser.getSelectedFile();
String name=file3.getName();
text3.setText(name);
}
}
});
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
Connection con = null;
String url = "jdbc:
mysql://localhost:3306/";;
String db = "register";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
FileInputStream fis1,fis2,fis3;
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
PreparedStatement st = con.prepareStatement("insert into person(image1,image2,image3) values(?,?,?)");
fis1 = new FileInputStream(file1);
fis2 = new FileInputStream(file2);
fis3 = new FileInputStream(file3);
st.setBinaryStream(1, (InputStream)fis1, (int)(file1.length()));
st.setBinaryStream(2, (InputStream)fis2, (int)(file2.length()));
st.setBinaryStream(3, (InputStream)fis3, (int)(file3.length()));
int s = st.executeUpdate();
JOptionPane.showMessageDialog(null,"images are successfully inserted.");
}
catch(Exception ex){}
}
});
JFrame f=new JFrame("Insert Image");
f.add(panel);
f.setVisible(true);
f.pack();
}
}
Thanks