Libraries used
import java.awt.Color; import java.awt.Font; import java.awt.event.KeyEvent; import java.awt.print.PrinterException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger;
Function to Retrieve data from product and insert into sales:
private void pos_sys(){
String name = txt_product_code.getText();
try {
Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root", "root");
PreparedStatement pt =co.prepareStatement("select * from products where ID= ? ");
pt.setString(1,name);
ResultSet rs = pt.executeQuery();
while(rs.next()){
int current_quantity;
current_quantity = rs.getInt("Quantity");
int price = Integer.parseInt(txt_price.getText());
int qtynew = Integer.parseInt(txt_quantity.getText());
int tot = price*qtynew;
if(qtynew > current_quantity){
JOptionPane.showMessageDialog(this, "Avaliable Product " + " = " + current_quantity);
JOptionPane.showMessageDialog(this, "Not Enough Quantity");
}
else{
DefaultTableModel model = (DefaultTableModel)tbl_data.getModel();
model.addRow(new Object[]
{
txt_product_code.getText(),
txt_product_name.getText(),
txt_price.getText(),
txt_quantity.getText(),
tot,});
}
int sum = 0 ;
for(int i =0 ; i < tbl_data.getRowCount(); i++){
sum = sum + Integer.parseInt(tbl_data.getValueAt(i, 4).toString());
}
txt_total.setText(Integer.toString(sum));
txt_product_code.setText("");
txt_product_name.setText("");
txt_price.setText("");
txt_quantity.setText("");
txt_product_code.requestFocus();
}
}
catch(Exception e){
e.printStackTrace();
}
}
int id =0;
public int getid()
{
ResultSet rs = null;
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root","root");
String query ="select max(id) from sale";
Statement st = con.createStatement();
rs = st.executeQuery(query);
while(rs.next()){
id=rs.getInt(1);
id++;
}
}
catch(Exception e){
e.printStackTrace();
}
return id;
}
public int receipt() {
int rec=0;
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root", "root");
PreparedStatement pst = con.prepareStatement("select max(id) from sale");
ResultSet rs = pst.executeQuery();
if(rs.next()){
rec = rs.getInt(1);
}
}
catch(Exception e){
e.printStackTrace();
}
return rec;
}
void sales() {
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDateTime now = LocalDateTime.now();
String date = dft.format(now);
String total = txt_total.getText();
String pay = txt_pay.getText();
String balance = txt_balance.getText();
int lastinsertid=0;
try{
//Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root","root");
String query ="Insert into sale(date,Subtotal,pay,Balance) values(?,?,?,?)";
PreparedStatement pst = con.prepareStatement(query,Statement.RETURN_GENERATED_KEYS);
// pst.setInt(1, getid());
pst.setString(1, date);
pst.setString(2, total);
pst.setString(3, pay);
pst.setString(4, balance);
pst.executeUpdate();
ResultSet gkr = pst.getGeneratedKeys();
// int i = pst.executeUpdate();
if(gkr.next()){
lastinsertid = gkr.getInt(1);
}
JOptionPane.showMessageDialog(this, lastinsertid);
String query2 ="Insert into sale_product(sale_id,product_id,sell_price,qty,total,pname) values(?,?,?,?,?,?)";
PreparedStatement pst2 = con.prepareStatement(query2);
String product_id="";
String price="";
String qty="";
String name="";
int totalp = 0;
for(int i = 0 ; i < tbl_data.getRowCount() ; i++){
product_id = (String)tbl_data.getValueAt(i, 0);
name= (String)tbl_data.getValueAt(i, 1);
price= (String)tbl_data.getValueAt(i, 2);
qty= (String)tbl_data.getValueAt(i, 3);
totalp= (int)tbl_data.getValueAt(i, 4);
pst2.setInt(1, lastinsertid);
pst2.setString(2, product_id);
pst2.setString(3, price);
pst2.setString(4, qty);
pst2.setInt(5, totalp);
pst2.setString(6, name);
pst2.executeUpdate();
}
String query3 = "Update products set Quantity = Quantity - ? where ID = ?";
PreparedStatement pst3 = con.prepareStatement(query3);
for(int i = 0 ; i < tbl_data.getRowCount() ; i++){
product_id = (String)tbl_data.getValueAt(i, 0);
qty= (String)tbl_data.getValueAt(i, 3);
pst3.setString(1,qty);
pst3.setString(2, product_id);
pst3.execute();
}
pst2.addBatch();
JOptionPane.showMessageDialog(this, "Record Sale successfull");
}
catch( Exception e){
e.printStackTrace();
}
}
public void Print()
{
String sub = txt_total.getText();
String pay = txt_pay.getText();
String balance = txt_balance.getText();
try {
new Print(sub,pay,balance,tbl_data.getModel()).setVisible(true);
} catch (PrinterException ex) {
Logger.getLogger(Pos.class.getName()).log(Level.SEVERE, null, ex);
}
}
Call function behind add button:
pos_sys();
Call piece of code behind Delete button:
DefaultTableModel model = (DefaultTableModel)tbl_data.getModel();
model.removeRow(tbl_data.getSelectedRow());
int sum = 0 ;
for(int i =0 ; i < tbl_data.getRowCount(); i++){
sum = sum + Integer.parseInt(tbl_data.getValueAt(i, 4).toString());
}
txt_total.setText(Integer.toString(sum));
Call piece of code behind Id text box:
if(evt.getKeyCode() == KeyEvent.VK_ENTER){ String name = txt_product_code.getText(); try { Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/store_managment_system","root", "root"); PreparedStatement pt =co.prepareStatement("select * from products where ID= ? "); pt.setString(1,name); ResultSet rs = pt.executeQuery(); if(rs.next()== false){ JOptionPane.showMessageDialog(this,"Product Not Found"); } else{ String productname = rs.getString("Pname"); String Price = rs.getString("Price"); txt_product_name.setText(productname.trim()); txt_price.setText(Price.trim()); txt_quantity.requestFocus(); } } catch (Exception ex) { ex.printStackTrace(); }






0 Comments