Project Source Code
Project Source Code
package java_project_1_2;
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;
}
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();
}
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;
}
}
if(imagePath != null)
{
myImage = new ImageIcon(imagePath);
}else{
myImage = new ImageIcon(pic);
}
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.get
String("add_date"),rs.getBytes("image"));
productList.add(product);
}
return productList;
model.addRow(row);
}
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() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
txt_AddDate.setDateFormatString("yyyy-MM-dd");
txt_AddDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
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);
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Align
ment.TRAILING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATE
D)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Align
ment.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.UNRELATE
D)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATE
D)
.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.UNRELATE
D)
.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.Align
ment.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.U
NRELATED)
.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.RELAT
ED)
.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.Align
ment.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))
);
pack();
}// </editor-fold>
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
ps.setString(3, addDate);
ps.setInt(4, Integer.parseInt(txt_id.getText()));
ps.executeUpdate();
Show_Products_In_JTable();
JOptionPane.showMessageDialog(null, "Product Updated");
}
// update With Image
else{
try{
InputStream img = new FileInputStream(new File(ImgPath));
ps = con.prepareStatement(UpdateQuery);
ps.setString(1, txt_name.getText());
ps.setString(2, txt_price.getText());
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");
}
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");
}
}
pos = 0;
ShowItem(pos);
}
pos = getProductList().size()-1;
ShowItem(pos);
}
pos++;
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.L
evel.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main_Window.class.getName()).log(java.util.logging.L
evel.SEVERE, null, ex);
}
//</editor-fold>
vb.net
Imports System.Data.SqlClient
Try
connection.Open()
' Définir la commande SQL de mise à jour
updateCommand.Parameters.AddWithValue("@QuantiteReduite", quantiteReduite)
updateCommand.Parameters.AddWithValue("@ProduitID", produitID)
updateCommand.ExecuteNonQuery()
connection.Close()
End Using
Catch ex As Exception
End Try
End Sub
ReduireQuantiteEnStock(produitID, quantiteReduite)
End Sub
End Class