NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package ImageToDB;

import java.awt.EventQueue;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;

public class ImageToDataBase {

private JFrame frame;
private JTextField textField;

File f = null;
String path = null;
private ImageIcon format = null;
String fname = null;
int s = 0;
byte[] pimage=null;


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ImageToDataBase window = new ImageToDataBase();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public ImageToDataBase() {
initialize();
Connect();
LoadImageID();
}
Connection con;
PreparedStatement st;
ResultSet rs;
public void Connect() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/image","root","");
}catch (ClassNotFoundException e) {
Logger.getLogger(ImageToDataBase.class.getName())
.log(Level.SEVERE, null, e);
}catch(SQLException ex) {
Logger.getLogger(ImageToDataBase.class.getName())
.log(Level.SEVERE, null, ex);
}

}
JComboBox comboBox = new JComboBox();
public void LoadImageID() {
try {
st = con.prepareStatement("SELECT id FROM images");
rs = st.executeQuery();
comboBox.removeAllItems();

while(rs.next()) {
comboBox.addItem(rs.getString(1));
}
}catch (SQLException ex) {
Logger.getLogger(ImageToDataBase.class.getName()).log(Level.SEVERE, null, ex);
}
}
JLabel lblNewLabel = new JLabel("");
public void LoadImage() {
try {
byte[] imagedata = rs.getBytes("imgFile");
format = new ImageIcon(imagedata);
Image mm = format.getImage();
Image img2 = mm.getScaledInstance(200, 200, Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(img2);
lblNewLabel.setIcon(image);
}catch (SQLException ex) {
Logger.getLogger(ImageToDataBase.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 527, 409);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);


springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 28, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 29, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel, 228, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, 229, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(lblNewLabel);


//Browse button
JButton btnNewButton = new JButton("Browse");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter fnef = new FileNameExtensionFilter
("PNG JPG JPEG","png","jpeg","jpg");
fileChooser.addChoosableFileFilter(fnef);
int load = fileChooser.showOpenDialog(null);
if(load==fileChooser.APPROVE_OPTION) {
f = fileChooser.getSelectedFile();
path = f.getAbsolutePath();
textField.setText(path);
ImageIcon ii = new ImageIcon(path);
Image img = ii.getImage().getScaledInstance
(200, 200, Image.SCALE_SMOOTH);
lblNewLabel.setIcon(new ImageIcon(img));
}
}
});



springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 46, SpringLayout.SOUTH, lblNewLabel);
springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 41, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(btnNewButton);

JButton btnNewButton_1 = new JButton("Insert");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("Image path - " + path);
File f = new File(path);
try {
InputStream is = new FileInputStream(f);
st = con.prepareStatement("INSERT INTO images"
+ "(imgName, imgPath, imgFile)VALUES(?,?,?)");
st.setString(1, f.getName());
st.setString(2, path);
st.setBlob(3, is);

int inserted = st.executeUpdate();
if(inserted>0) {
JOptionPane.showMessageDialog(null, "Successfully added");
LoadImageID();
}
} catch (Exception e2) {
// TODO: handle exception
}
}
});

springLayout.putConstraint(SpringLayout.NORTH, btnNewButton_1, 0, SpringLayout.NORTH, btnNewButton);
springLayout.putConstraint(SpringLayout.WEST, btnNewButton_1, 34, SpringLayout.EAST, btnNewButton);
frame.getContentPane().add(btnNewButton_1);

textField = new JTextField();
springLayout.putConstraint(SpringLayout.WEST, textField, 41, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, textField, -11, SpringLayout.NORTH, btnNewButton);
springLayout.putConstraint(SpringLayout.EAST, textField, 241, SpringLayout.WEST, frame.getContentPane());
frame.getContentPane().add(textField);
textField.setColumns(10);
//Search Button
JButton btnNewButton_2 = new JButton("Search");
btnNewButton_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String imgID = comboBox.getSelectedItem().toString();
try {
st = con.prepareStatement("SELECT * FROM images WHERE id =?");
st.setString(1, imgID);
rs = st.executeQuery();
if(rs.next()) {
LoadImage();

}else {
JOptionPane.showMessageDialog(null, "NO image found");
}
}catch (Exception ex) {
//TODO: handle exception
}
}
});
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton_2, 26, SpringLayout.SOUTH, btnNewButton_1);
springLayout.putConstraint(SpringLayout.EAST, btnNewButton_2, 0, SpringLayout.EAST, btnNewButton_1);
frame.getContentPane().add(btnNewButton_2);

springLayout.putConstraint(SpringLayout.NORTH, comboBox, 26, SpringLayout.SOUTH, btnNewButton);
springLayout.putConstraint(SpringLayout.WEST, comboBox, 0, SpringLayout.WEST, btnNewButton);
springLayout.putConstraint(SpringLayout.EAST, comboBox, -30, SpringLayout.WEST, btnNewButton_2);
frame.getContentPane().add(comboBox);
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.