Java And MySQL - Project Example With Source Code
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
-MySQL Database .
project demonstration
Project Tutorials List:
- Part 1
- Part 2
- Part 3
- Part 4
- Part 5
- Part 6
- Part 7
- Part 8
- Part 9
- Part 10
- Part 11
Project Source Code:
First Creat Product Class
Start Product Class
package java_project_1_2;
public class Product {
private int id;
private String name;
private float price;
private String addDate;
private byte[] picture;
public Product(int pid, String pname, float pprice, String pAddDate, byte[] pimg)
{
this.id = pid;
this.name = pname;
this.price = pprice;
this.addDate = pAddDate;
this.picture = pimg;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public float getPrice()
{
return price;
}
public String getAddDate()
{
return addDate;
}
public byte[] getImage()
{
return picture;
}
}
EndProduct Class
package java_project_1_2;
import java.awt.Image;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
/**
*
* @author 1BestCsharp.blogspot.com
*/
public class Main_Window extends javax.swing.JFrame {
/**
* Creates new form Main_Window
*/
public Main_Window() {
initComponents();
Show_Products_In_JTable();
}
String ImgPath = null;
int pos = 0;
// Function To Connect To MySQL Database
public Connection getConnection()
{
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/products_db2","root","");
return con;
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
// Check Input Fields
public boolean checkInputs()
{
if(
txt_name.getText() == null
|| txt_price.getText() == null
|| txt_AddDate.getDate() == null
){
return false;
}
else{
try{
Float.parseFloat(txt_price.getText());
return true;
}catch(Exception ex)
{
return false;
}
}
}
// Function To Resize The Image To Fit Into JLabel
public ImageIcon ResizeImage(String imagePath, byte[] pic)
{
ImageIcon myImage = null;
if(imagePath != null)
{
myImage = new ImageIcon(imagePath);
}else{
myImage = new ImageIcon(pic);
}
Image img = myImage.getImage();
Image img2 = img.getScaledInstance(lbl_image.getWidth(), lbl_image.getHeight(), Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(img2);
return image;
}
// Display Data In JTable:
// 1 - Fill ArrayList With The Data
public ArrayList<Product> getProductList()
{
ArrayList<Product> productList = new ArrayList<Product>();
Connection con = getConnection();
String query = "SELECT * FROM products";
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery(query);
Product product;
while(rs.next())
{
product = new Product(rs.getInt("id"),rs.getString("name"),Float.parseFloat(rs.getString("price")),rs.getString("add_date"),rs.getBytes("image"));
productList.add(product);
}
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
return productList;
}
// 2 - Populate The JTable
public void Show_Products_In_JTable()
{
ArrayList<Product> list = getProductList();
DefaultTableModel model = (DefaultTableModel)JTable_Products.getModel();
// clear jtable content
model.setRowCount(0);
Object[] row = new Object[4];
for(int i = 0; i < list.size(); i++)
{
row[0] = list.get(i).getId();
row[1] = list.get(i).getName();
row[2] = list.get(i).getPrice();
row[3] = list.get(i).getAddDate();
model.addRow(row);
}
}
// Show Data In Inputs
public void ShowItem(int index)
{
txt_id.setText(Integer.toString(getProductList().get(index).getId()));
txt_name.setText(getProductList().get(index).getName());
txt_price.setText(Float.toString(getProductList().get(index).getPrice()));
try {
Date addDate = null;
addDate = new SimpleDateFormat("yyyy-MM-dd").parse((String)getProductList().get(index).getAddDate());
txt_AddDate.setDate(addDate);
} catch (ParseException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
lbl_image.setIcon(ResizeImage(null, getProductList().get(index).getImage()));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txt_name = new javax.swing.JTextField();
txt_id = new javax.swing.JTextField();
txt_price = new javax.swing.JTextField();
txt_AddDate = new com.toedter.calendar.JDateChooser();
lbl_image = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
JTable_Products = new javax.swing.JTable();
Btn_Choose_Image = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
Btn_Insert = new javax.swing.JButton();
Btn_First = new javax.swing.JButton();
Btn_Previous = new javax.swing.JButton();
Btn_Last = new javax.swing.JButton();
Btn_Next = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 204));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("ID:");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel2.setText("Name:");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel3.setText("Price:");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel4.setText("Add Date:");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel5.setText("Image:");
txt_name.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
txt_name.setPreferredSize(new java.awt.Dimension(59, 50));
txt_id.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
txt_id.setEnabled(false);
txt_id.setPreferredSize(new java.awt.Dimension(59, 50));
txt_price.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
txt_price.setPreferredSize(new java.awt.Dimension(59, 50));
txt_AddDate.setDateFormatString("yyyy-MM-dd");
txt_AddDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
lbl_image.setBackground(new java.awt.Color(204, 255, 255));
lbl_image.setOpaque(true);
JTable_Products.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ID", "Name", "Price", "Add Date"
}
));
JTable_Products.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
JTable_ProductsMouseClicked(evt);
}
});
jScrollPane1.setViewportView(JTable_Products);
Btn_Choose_Image.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
Btn_Choose_Image.setText("Choose Image");
Btn_Choose_Image.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_Choose_ImageActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/Renew.png"))); // NOI18N
jButton2.setText("Update");
jButton2.setIconTextGap(15);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/delete.png"))); // NOI18N
jButton3.setText("Delete");
jButton3.setIconTextGap(15);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
Btn_Insert.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Insert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/add.png"))); // NOI18N
Btn_Insert.setText("Insert");
Btn_Insert.setIconTextGap(15);
Btn_Insert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_InsertActionPerformed(evt);
}
});
Btn_First.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_First.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/first.png"))); // NOI18N
Btn_First.setText("First");
Btn_First.setIconTextGap(15);
Btn_First.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_FirstActionPerformed(evt);
}
});
Btn_Previous.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Previous.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/previous.png"))); // NOI18N
Btn_Previous.setText("Previous");
Btn_Previous.setIconTextGap(15);
Btn_Previous.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_PreviousActionPerformed(evt);
}
});
Btn_Last.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Last.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/last.png"))); // NOI18N
Btn_Last.setText("Last");
Btn_Last.setIconTextGap(15);
Btn_Last.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_LastActionPerformed(evt);
}
});
Btn_Next.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/next.png"))); // NOI18N
Btn_Next.setText("Next");
Btn_Next.setIconTextGap(15);
Btn_Next.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_NextActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Btn_Choose_Image, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
.addComponent(txt_AddDate, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt_name, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txt_price, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbl_image, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(Btn_Insert)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addGap(45, 45, 45)
.addComponent(Btn_First)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btn_Next)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btn_Previous)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Btn_Last)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(9, 9, 9)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txt_price, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel4))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txt_AddDate, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(lbl_image, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btn_Choose_Image, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Btn_Insert, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_First, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_Next, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_Previous, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_Last, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
// Button Browse Image From Your Computer
private void Btn_Choose_ImageActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.images", "jpg","png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
{
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
lbl_image.setIcon(ResizeImage(path, null));
ImgPath = path;
}
else{
System.out.println("No File Selected");
}
}
// Button Insert Data Into MySQL Database
// 1 - Check If The imgPath Is Not Null And The Inputs Are Not Empty
// 2 - Insert The Data
private void Btn_InsertActionPerformed(java.awt.event.ActionEvent evt) {
if(checkInputs() && ImgPath != null)
{
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO products(name,price,add_date,image)"
+ "values(?,?,?,?) ");
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(txt_AddDate.getDate());
ps.setString(3, addDate);
InputStream img = new FileInputStream(new File(ImgPath));
ps.setBlob(4, img);
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Data Inserted");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}else{
JOptionPane.showMessageDialog(null, "One Or More Field Are Empty");
}
// only for test
System.out.println("Name => "+txt_name.getText());
System.out.println("Price => "+txt_price.getText());
System.out.println("Image => "+ImgPath);
}
// Button Update Data From MySQL Database
// 1 - Check If Inputs Is Not Null
// If The imgPath Is Not Null Update Also The Image
// else don't update the Image
// 2 - Update The Data
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(checkInputs() && txt_id.getText() != null)
{
String UpdateQuery = null;
PreparedStatement ps = null;
Connection con = getConnection();
// update without image
if(ImgPath == null)
{
try {
UpdateQuery = "UPDATE products SET name = ?, price = ?"
+ ", add_date = ? WHERE id = ?";
ps = con.prepareStatement(UpdateQuery);
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(txt_AddDate.getDate());
ps.setString(3, addDate);
ps.setInt(4, Integer.parseInt(txt_id.getText()));
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Updated");
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
// update With Image
else{
try{
InputStream img = new FileInputStream(new File(ImgPath));
UpdateQuery = "UPDATE products SET name = ?, price = ?"
+ ", add_date = ?, image = ? WHERE id = ?";
ps = con.prepareStatement(UpdateQuery);
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(txt_AddDate.getDate());
ps.setString(3, addDate);
ps.setBlob(4, img);
ps.setInt(5, Integer.parseInt(txt_id.getText()));
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Updated");
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}else{
JOptionPane.showMessageDialog(null, "One Or More Fields Are Empty Or Wrong");
}
}
// Button Delete The Data From MySQL Database
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if(!txt_id.getText().equals(""))
{
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM products WHERE id = ?");
int id = Integer.parseInt(txt_id.getText());
ps.setInt(1, id);
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Deleted");
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Product Not Deleted");
}
}else{
JOptionPane.showMessageDialog(null, "Product Not Deleted : No Id To Delete");
}
}
// JTable Mouse Clicked
// Display The Selected Row Data Into JTextFields
// And The Image Into JLabel
private void JTable_ProductsMouseClicked(java.awt.event.MouseEvent evt) {
int index = JTable_Products.getSelectedRow();
ShowItem(index);
}
// Button First Show The First Record
private void Btn_FirstActionPerformed(java.awt.event.ActionEvent evt) {
pos = 0;
ShowItem(pos);
}
// Button Last Show The Last Record
private void Btn_LastActionPerformed(java.awt.event.ActionEvent evt) {
pos = getProductList().size()-1;
ShowItem(pos);
}
// Button Next Show The Next Record
private void Btn_NextActionPerformed(java.awt.event.ActionEvent evt) {
pos++;
if(pos >= getProductList().size())
{
pos = getProductList().size()-1;
}
ShowItem(pos);
}
// Button Previous Show The Previous Record
private void Btn_PreviousActionPerformed(java.awt.event.ActionEvent evt) {
pos--;
if(pos < 0)
{
pos = 0;
}
ShowItem(pos);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main_Window().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Btn_Choose_Image;
private javax.swing.JButton Btn_First;
private javax.swing.JButton Btn_Insert;
private javax.swing.JButton Btn_Last;
private javax.swing.JButton Btn_Next;
private javax.swing.JButton Btn_Previous;
private javax.swing.JTable JTable_Products;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbl_image;
private com.toedter.calendar.JDateChooser txt_AddDate;
private javax.swing.JTextField txt_id;
private javax.swing.JTextField txt_name;
private javax.swing.JTextField txt_price;
// End of variables declaration
}
import java.awt.Image;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
/**
*
* @author 1BestCsharp.blogspot.com
*/
public class Main_Window extends javax.swing.JFrame {
/**
* Creates new form Main_Window
*/
public Main_Window() {
initComponents();
Show_Products_In_JTable();
}
String ImgPath = null;
int pos = 0;
// Function To Connect To MySQL Database
public Connection getConnection()
{
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost/products_db2","root","");
return con;
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
// Check Input Fields
public boolean checkInputs()
{
if(
txt_name.getText() == null
|| txt_price.getText() == null
|| txt_AddDate.getDate() == null
){
return false;
}
else{
try{
Float.parseFloat(txt_price.getText());
return true;
}catch(Exception ex)
{
return false;
}
}
}
// Function To Resize The Image To Fit Into JLabel
public ImageIcon ResizeImage(String imagePath, byte[] pic)
{
ImageIcon myImage = null;
if(imagePath != null)
{
myImage = new ImageIcon(imagePath);
}else{
myImage = new ImageIcon(pic);
}
Image img = myImage.getImage();
Image img2 = img.getScaledInstance(lbl_image.getWidth(), lbl_image.getHeight(), Image.SCALE_SMOOTH);
ImageIcon image = new ImageIcon(img2);
return image;
}
// Display Data In JTable:
// 1 - Fill ArrayList With The Data
public ArrayList<Product> getProductList()
{
ArrayList<Product> productList = new ArrayList<Product>();
Connection con = getConnection();
String query = "SELECT * FROM products";
Statement st;
ResultSet rs;
try {
st = con.createStatement();
rs = st.executeQuery(query);
Product product;
while(rs.next())
{
product = new Product(rs.getInt("id"),rs.getString("name"),Float.parseFloat(rs.getString("price")),rs.getString("add_date"),rs.getBytes("image"));
productList.add(product);
}
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
return productList;
}
// 2 - Populate The JTable
public void Show_Products_In_JTable()
{
ArrayList<Product> list = getProductList();
DefaultTableModel model = (DefaultTableModel)JTable_Products.getModel();
// clear jtable content
model.setRowCount(0);
Object[] row = new Object[4];
for(int i = 0; i < list.size(); i++)
{
row[0] = list.get(i).getId();
row[1] = list.get(i).getName();
row[2] = list.get(i).getPrice();
row[3] = list.get(i).getAddDate();
model.addRow(row);
}
}
// Show Data In Inputs
public void ShowItem(int index)
{
txt_id.setText(Integer.toString(getProductList().get(index).getId()));
txt_name.setText(getProductList().get(index).getName());
txt_price.setText(Float.toString(getProductList().get(index).getPrice()));
try {
Date addDate = null;
addDate = new SimpleDateFormat("yyyy-MM-dd").parse((String)getProductList().get(index).getAddDate());
txt_AddDate.setDate(addDate);
} catch (ParseException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
lbl_image.setIcon(ResizeImage(null, getProductList().get(index).getImage()));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txt_name = new javax.swing.JTextField();
txt_id = new javax.swing.JTextField();
txt_price = new javax.swing.JTextField();
txt_AddDate = new com.toedter.calendar.JDateChooser();
lbl_image = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
JTable_Products = new javax.swing.JTable();
Btn_Choose_Image = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
Btn_Insert = new javax.swing.JButton();
Btn_First = new javax.swing.JButton();
Btn_Previous = new javax.swing.JButton();
Btn_Last = new javax.swing.JButton();
Btn_Next = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(255, 255, 204));
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel1.setText("ID:");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel2.setText("Name:");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel3.setText("Price:");
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel4.setText("Add Date:");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel5.setText("Image:");
txt_name.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
txt_name.setPreferredSize(new java.awt.Dimension(59, 50));
txt_id.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
txt_id.setEnabled(false);
txt_id.setPreferredSize(new java.awt.Dimension(59, 50));
txt_price.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
txt_price.setPreferredSize(new java.awt.Dimension(59, 50));
txt_AddDate.setDateFormatString("yyyy-MM-dd");
txt_AddDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
lbl_image.setBackground(new java.awt.Color(204, 255, 255));
lbl_image.setOpaque(true);
JTable_Products.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"ID", "Name", "Price", "Add Date"
}
));
JTable_Products.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
JTable_ProductsMouseClicked(evt);
}
});
jScrollPane1.setViewportView(JTable_Products);
Btn_Choose_Image.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
Btn_Choose_Image.setText("Choose Image");
Btn_Choose_Image.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_Choose_ImageActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/Renew.png"))); // NOI18N
jButton2.setText("Update");
jButton2.setIconTextGap(15);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jButton3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/delete.png"))); // NOI18N
jButton3.setText("Delete");
jButton3.setIconTextGap(15);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
Btn_Insert.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Insert.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/add.png"))); // NOI18N
Btn_Insert.setText("Insert");
Btn_Insert.setIconTextGap(15);
Btn_Insert.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_InsertActionPerformed(evt);
}
});
Btn_First.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_First.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/first.png"))); // NOI18N
Btn_First.setText("First");
Btn_First.setIconTextGap(15);
Btn_First.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_FirstActionPerformed(evt);
}
});
Btn_Previous.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Previous.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/previous.png"))); // NOI18N
Btn_Previous.setText("Previous");
Btn_Previous.setIconTextGap(15);
Btn_Previous.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_PreviousActionPerformed(evt);
}
});
Btn_Last.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Last.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/last.png"))); // NOI18N
Btn_Last.setText("Last");
Btn_Last.setIconTextGap(15);
Btn_Last.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_LastActionPerformed(evt);
}
});
Btn_Next.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
Btn_Next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/JAVA_VIDEOS_TUTORIALS/icons/next.png"))); // NOI18N
Btn_Next.setText("Next");
Btn_Next.setIconTextGap(15);
Btn_Next.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Btn_NextActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(Btn_Choose_Image, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
.addComponent(txt_AddDate, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txt_name, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(txt_price, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lbl_image, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(Btn_Insert)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addGap(45, 45, 45)
.addComponent(Btn_First)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btn_Next)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btn_Previous)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Btn_Last)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_id, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(9, 9, 9)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txt_name, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(14, 14, 14)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(txt_price, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel4))
.addGroup(jPanel1Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(txt_AddDate, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(lbl_image, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Btn_Choose_Image, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(Btn_Insert, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_First, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_Next, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_Previous, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Btn_Last, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(31, 31, 31))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
// Button Browse Image From Your Computer
private void Btn_Choose_ImageActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.images", "jpg","png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
{
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
lbl_image.setIcon(ResizeImage(path, null));
ImgPath = path;
}
else{
System.out.println("No File Selected");
}
}
// Button Insert Data Into MySQL Database
// 1 - Check If The imgPath Is Not Null And The Inputs Are Not Empty
// 2 - Insert The Data
private void Btn_InsertActionPerformed(java.awt.event.ActionEvent evt) {
if(checkInputs() && ImgPath != null)
{
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO products(name,price,add_date,image)"
+ "values(?,?,?,?) ");
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(txt_AddDate.getDate());
ps.setString(3, addDate);
InputStream img = new FileInputStream(new File(ImgPath));
ps.setBlob(4, img);
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Data Inserted");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}else{
JOptionPane.showMessageDialog(null, "One Or More Field Are Empty");
}
// only for test
System.out.println("Name => "+txt_name.getText());
System.out.println("Price => "+txt_price.getText());
System.out.println("Image => "+ImgPath);
}
// Button Update Data From MySQL Database
// 1 - Check If Inputs Is Not Null
// If The imgPath Is Not Null Update Also The Image
// else don't update the Image
// 2 - Update The Data
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if(checkInputs() && txt_id.getText() != null)
{
String UpdateQuery = null;
PreparedStatement ps = null;
Connection con = getConnection();
// update without image
if(ImgPath == null)
{
try {
UpdateQuery = "UPDATE products SET name = ?, price = ?"
+ ", add_date = ? WHERE id = ?";
ps = con.prepareStatement(UpdateQuery);
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(txt_AddDate.getDate());
ps.setString(3, addDate);
ps.setInt(4, Integer.parseInt(txt_id.getText()));
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Updated");
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
// update With Image
else{
try{
InputStream img = new FileInputStream(new File(ImgPath));
UpdateQuery = "UPDATE products SET name = ?, price = ?"
+ ", add_date = ?, image = ? WHERE id = ?";
ps = con.prepareStatement(UpdateQuery);
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String addDate = dateFormat.format(txt_AddDate.getDate());
ps.setString(3, addDate);
ps.setBlob(4, img);
ps.setInt(5, Integer.parseInt(txt_id.getText()));
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Updated");
}catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}else{
JOptionPane.showMessageDialog(null, "One Or More Fields Are Empty Or Wrong");
}
}
// Button Delete The Data From MySQL Database
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if(!txt_id.getText().equals(""))
{
try {
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM products WHERE id = ?");
int id = Integer.parseInt(txt_id.getText());
ps.setInt(1, id);
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Deleted");
} catch (SQLException ex) {
Logger.getLogger(Main_Window.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, "Product Not Deleted");
}
}else{
JOptionPane.showMessageDialog(null, "Product Not Deleted : No Id To Delete");
}
}
// JTable Mouse Clicked
// Display The Selected Row Data Into JTextFields
// And The Image Into JLabel
private void JTable_ProductsMouseClicked(java.awt.event.MouseEvent evt) {
int index = JTable_Products.getSelectedRow();
ShowItem(index);
}
// Button First Show The First Record
private void Btn_FirstActionPerformed(java.awt.event.ActionEvent evt) {
pos = 0;
ShowItem(pos);
}
// Button Last Show The Last Record
private void Btn_LastActionPerformed(java.awt.event.ActionEvent evt) {
pos = getProductList().size()-1;
ShowItem(pos);
}
// Button Next Show The Next Record
private void Btn_NextActionPerformed(java.awt.event.ActionEvent evt) {
pos++;
if(pos >= getProductList().size())
{
pos = getProductList().size()-1;
}
ShowItem(pos);
}
// Button Previous Show The Previous Record
private void Btn_PreviousActionPerformed(java.awt.event.ActionEvent evt) {
pos--;
if(pos < 0)
{
pos = 0;
}
ShowItem(pos);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main_Window().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Btn_Choose_Image;
private javax.swing.JButton Btn_First;
private javax.swing.JButton Btn_Insert;
private javax.swing.JButton Btn_Last;
private javax.swing.JButton Btn_Next;
private javax.swing.JButton Btn_Previous;
private javax.swing.JTable JTable_Products;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbl_image;
private com.toedter.calendar.JDateChooser txt_AddDate;
private javax.swing.JTextField txt_id;
private javax.swing.JTextField txt_name;
private javax.swing.JTextField txt_price;
// End of variables declaration
}
if you want the source code click on the download button below
disclaimer: you will get the source code without the database script.
More Java Projects:
- Java Student Information System Source Code
- Java Library Management System Source Code
- Java Contact Information Management System Source Code
- Java Inventory System Source Code
- Java Hotel Management System Source Code
- Java Real Estate Management System
- Java Contact Information Management System Source Code
- Java Inventory System Source Code
- Java Hotel Management System Source Code
- Java Real Estate Management System
Download Projects Source Code
4 comments
commentsplease ..... where do i find the download link for the source code
ReplyPlzz give me download link..
ReplyPlzz give me download link..
Replyplease can you give the link
Reply