NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package java1;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;

public class finallly {

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;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
finallly window = new finallly();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public finallly() {
initialize();
LoadImageID();
Connect();
}
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);
}

}
Connection con;
ResultSet rs;
PreparedStatement st;
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);
}
}
/**
* Initialize the contents of the frame.
*/
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, 1000, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout springLayout = new SpringLayout();
frame.getContentPane().setLayout(springLayout);


JScrollPane sp = new JScrollPane();
springLayout.putConstraint(SpringLayout.NORTH, sp, 45, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, sp, 120, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, sp, 248, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, sp, 500, SpringLayout.WEST, frame.getContentPane());
sp.setBounds(10,64,366,107);
frame.getContentPane().add(sp);

JTextField username = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, username, 50, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, username, 10, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, username, 78, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, username, 100, SpringLayout.WEST, frame.getContentPane());
username.setBounds(10,184,198,28);
frame.getContentPane().add(username);

JTextField emaile = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, emaile, 100, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.WEST, emaile, 10, SpringLayout.WEST, frame.getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, emaile, 128, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, emaile, 100, SpringLayout.WEST, frame.getContentPane());
emaile.setBounds(10,234,198,28);
frame.getContentPane().add(emaile);

try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/cs2a","root","");
Statement stmt = con.createStatement();
String sql = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(sql);

String[] fcolumn = {"ID","Username","Email"};
DefaultTableModel tableModel = new DefaultTableModel(fcolumn,0);

while(rs.next()) {
String id = String.valueOf(rs.getInt("uid"));
String uname = rs.getString("username");
String email = rs.getString("email");
String[] data = {id, uname, email };
tableModel.addRow(data);
}
JTable jt = new JTable(tableModel);
sp.setViewportView(jt);

jt.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int row = jt.getSelectedRow();
if(row<0) {
System.out.print("test");
}else {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/cs2a","root","");
Statement stmt = con.createStatement();

String id = jt.getValueAt(row, 0).toString();
String sql = "SELECT * FROM users WHERE uid='"+id+"'";
ResultSet rs = stmt.executeQuery(sql);
if(rs.next()) {
username.setText(rs.getString("username"));
emaile.setText(rs.getString("email"));
}

} catch (Exception e) {
System.out.print(e);
}
}
}
});
JButton btnUpdate = new JButton("Update");
springLayout.putConstraint(SpringLayout.NORTH, btnUpdate, 18, SpringLayout.SOUTH, emaile);
springLayout.putConstraint(SpringLayout.WEST, btnUpdate, 22, SpringLayout.WEST, frame.getContentPane());
btnUpdate.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int row = jt.getSelectedRow();
if(row<0) {
System.out.print("test");
}else {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/cs2a","root","");
Statement stmt = con.createStatement();
String id = jt.getValueAt(row, 0).toString();
String sql = "UPDATE users SET username='" + username.getText() + "', email='" + emaile.getText() + "' WHERE uid='" + id + "'";
stmt.execute(sql);
con.close();
}catch(Exception ex) {System.out.print(ex);}
}
}
});
btnUpdate.setBounds(65,274,89,23);
frame.getContentPane().add(btnUpdate);
con.close();

JButton btnDelete = new JButton("Delete");
springLayout.putConstraint(SpringLayout.NORTH, btnDelete, 6, SpringLayout.SOUTH, btnUpdate);
springLayout.putConstraint(SpringLayout.WEST, btnDelete, 0, SpringLayout.WEST, btnUpdate);
springLayout.putConstraint(SpringLayout.EAST, btnDelete, 0, SpringLayout.EAST, btnUpdate);
btnDelete.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int row = jt.getSelectedRow();
if(row<0) {
System.out.print("test");
}else {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/cs2a","root","");
Statement stmt = con.createStatement();
String id = jt.getValueAt(row, 0).toString();
String sql = "DELETE FROM users WHERE uid='"+id+"'";
stmt.execute(sql);
con.close();
}catch(Exception ex) {System.out.print(ex);}
}
}
});
btnDelete.setBounds(255,274,89,23);
frame.getContentPane().add(btnDelete);
con.close();
}catch(Exception ex) {System.out.print(ex);}

JButton btnNewButton = new JButton("Refresh");
springLayout.putConstraint(SpringLayout.NORTH, btnNewButton, 28, SpringLayout.SOUTH, sp);
springLayout.putConstraint(SpringLayout.WEST, btnNewButton, 272, SpringLayout.WEST, frame.getContentPane());
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
frame.getContentPane().add(btnNewButton);

springLayout.putConstraint(SpringLayout.NORTH, lblNewLabel, 0, SpringLayout.NORTH, username);
springLayout.putConstraint(SpringLayout.WEST, lblNewLabel, 50, SpringLayout.EAST, sp);
springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel, 200, SpringLayout.NORTH, frame.getContentPane());
springLayout.putConstraint(SpringLayout.EAST, lblNewLabel, -280, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(lblNewLabel);


textField = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, textField, 19, SpringLayout.SOUTH, lblNewLabel);
springLayout.putConstraint(SpringLayout.WEST, textField, 0, SpringLayout.WEST, lblNewLabel);
springLayout.putConstraint(SpringLayout.EAST, textField, -280, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(textField);
textField.setColumns(10);

JButton btnNewButton_1 = new JButton("Browse");
btnNewButton_1.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_1, 17, SpringLayout.SOUTH, textField);
springLayout.putConstraint(SpringLayout.EAST, btnNewButton_1, -363, SpringLayout.EAST, frame.getContentPane());
frame.getContentPane().add(btnNewButton_1);

JButton btnNewButton_2 = new JButton("Insert");
btnNewButton_2.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.WEST, btnNewButton_2, 18, SpringLayout.EAST, btnNewButton_1);
springLayout.putConstraint(SpringLayout.SOUTH, btnNewButton_2, 0, SpringLayout.SOUTH, btnNewButton_1);
frame.getContentPane().add(btnNewButton_2);

JButton btnNewButton_3 = new JButton("Search");
btnNewButton_3.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_3, 19, SpringLayout.SOUTH, btnNewButton_2);
springLayout.putConstraint(SpringLayout.EAST, btnNewButton_3, 0, SpringLayout.EAST, btnNewButton_2);
frame.getContentPane().add(btnNewButton_3);

springLayout.putConstraint(SpringLayout.WEST, comboBox, 0, SpringLayout.WEST, btnNewButton_1);
springLayout.putConstraint(SpringLayout.SOUTH, comboBox, 0, SpringLayout.SOUTH, btnNewButton_3);
springLayout.putConstraint(SpringLayout.EAST, comboBox, -363, SpringLayout.EAST, frame.getContentPane());
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.