// your code goes here
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hcl
*/
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.sql.*;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import sun.jdbc.odbc.*;
public class First {
JFrame guiFrame1 = new JFrame();
JFrame guiFrame2 = new JFrame();
JFrame guiFrame3 = new JFrame();
JFrame guiFrame4 = new JFrame();
JFrame guiFrame5=new JFrame();
JFrame guiFrame6=new JFrame();
JFrame guiFrame7=new JFrame();
JFrame guiFrame8=new JFrame();
JLabel newline1=new JLabel(" ");
JLabel label1=new JLabel("This is a stationry shop management software." );
JLabel label2=new JLabel("It will be helpful for shop keepers to store and billing the stock details." );
JLabel label21 =new JLabel("It also be helpful to update and delete stock.");
JLabel label22=new JLabel("The Shopkeeper can view the sock details through this application");
JLabel label3=new JLabel("To store the new stock details ");
JLabel label4=new JLabel("To delete the stock details ");
JLabel label5=new JLabel("To billing the stock details ");
JLabel label6=new JLabel(" Store the item ");
JLabel label7=new JLabel("Enter Itemcode ");
JLabel label8=new JLabel("Enter name ");
JLabel label10=new JLabel("Enter Rate ");
JLabel label9=new JLabel("Enter Quantity ");
JLabel label11=new JLabel("Enter Itemname ");
JLabel label12=new JLabel("To update the stock details ");
JLabel label13=new JLabel("Select itemname ");
JLabel label14=new JLabel(" Rate ");
JLabel label15=new JLabel(" Quantity ");
JLabel total=new JLabel("Total: ");
JLabel amount=new JLabel();
JLabel view=new JLabel("To view the stock details ");
JTextField textfield1=new JTextField(10);
JTextField textfield2=new JTextField(10);
JTextField textfield3=new JTextField(10);
JTextField textfield4=new JTextField(10);
JTextField textfield5=new JTextField(10);
JTextField textfield6=new JTextField(16);
JTextField textfield7=new JTextField(16);
JButton click = new JButton( "click to procceed");
JButton store = new JButton( "Store");
JButton delete = new JButton( "Delete");
JButton update = new JButton( "Update");
JButton billing = new JButton( "Billing");
JButton items=new JButton("Ok");
JButton deleteitem=new JButton("Ok");
JButton save=new JButton("Save");
JButton addrow = new JButton("Add row");
JButton calculate=new JButton("Calculate Price");
JButton deleterow = new JButton("Delete row");
JButton viewstock=new JButton("View ");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JPanel panel5=new JPanel();
JPanel panel6=new JPanel();
JPanel panel7=new JPanel();
JPanel panel8=new JPanel();
JComboBox combo1=new JComboBox();
JTable table;
JTable table1;
DefaultTableModel model = new DefaultTableModel();
DefaultTableModel model1=new DefaultTableModel();
JScrollPane scroll;
JScrollPane scroll1;
String temp1;
String temp2;
String temp3;
String temp4;
String temp5;
String data[];
String temp6;
String temp7;
String temp8;
String temp9;
String temp10;
String name;
String rate;
String quantity;
int a;
int b;
int i=0;
int j=1;
int k=2;
int q=3;
int p=0;
Connection c;
String name1 = "test";
String pass = "test";
String url = "jdbc:odbc:shop";
public First()
{
//make sure the program exits when the frame closes
//guiFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame1.setTitle("Stock Management");
guiFrame1.setSize(450,300);
//This will center the JFrame in the middle of the screen
guiFrame1.setLocationRelativeTo(null);
guiFrame1.setVisible(true);
panel1.add(newline1);
panel1.add(label1,BorderLayout.LINE_START);
panel1.add(label2,BorderLayout.AFTER_LAST_LINE);
panel1.add(label21);
panel1.add(label22);
panel1.add(click,BorderLayout.AFTER_LAST_LINE);
guiFrame1.add(panel1);
click.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
//guiFrame2.setDefaultCloseOperation(;
guiFrame2.setTitle("Stock Management");
guiFrame2.setSize(275,250);
//This will center the JFrame in the middle of the screen
guiFrame2.setLocationRelativeTo(null);
guiFrame2.setVisible(true);
panel2.add(newline1);
panel2.add(label3,BorderLayout.PAGE_START);
panel2.add(store);
panel2.add(label12);
panel2.add(update);
panel2.add(label4);
panel2.add(delete);
panel2.add(label5);
panel2.add(billing);
panel2.add(view);
panel2.add(viewstock);
guiFrame2.add(panel2);
}
});
store.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
// guiFrame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame4.setTitle("Store Items");
guiFrame4.setSize(270,250);
//This will center the JFrame in the middle of the screen
guiFrame4.setLocationRelativeTo(null);
guiFrame4.setVisible(true);
panel4.add(newline1);
panel4.add(label6);
panel4.add(newline1);
panel4.add(label7);
panel4.add(textfield1);
panel4.add(label8);
panel4.add(textfield2);
panel4.add(label10);
panel4.add(textfield3);
panel4.add(label9);
panel4.add(textfield4);
panel4.add(items);
guiFrame4.add(panel4);
}
});
items.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
try {
System.out.println("connecting to the " + name1 + " data source...");
temp1=textfield1.getText();
temp2=textfield2.getText();
temp3=textfield3.getText();
temp4=textfield4.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st = c.createStatement();
System.out.println(temp1+temp2+temp3+temp4);
st.executeUpdate("insert into stock values ("+temp1+",'"+temp2+"',"+temp3+","+temp4+")");
//joptionpane.showmessagedialog("item is stored in the database!");
textfield1.setText("");
textfield2.setText("");
textfield3.setText("");
textfield4.setText("");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
delete.addActionListener(new ActionListener()
{
//@Override
ResultSet rs0;
public void actionPerformed(ActionEvent event)
{
// guiFrame5.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame5.setTitle("Delete Items");
guiFrame5.setSize(275,250);
//This will center the JFrame in the middle of the screen
guiFrame5.setLocationRelativeTo(null);
guiFrame5.setVisible(true);
panel5.add(newline1);
panel5.add(label11);
panel5.add(textfield5);
panel5.add(deleteitem);
guiFrame5.add(panel5);
}
});
deleteitem.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
try {
System.out.println("connecting to the " + name1 + " data source...");
temp5=textfield5.getText();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st1 = c.createStatement();
System.out.println(temp5);
String sql2 = "Delete from stock where itemname = '"+(temp5)+"'";
st1.executeUpdate(sql2);
JOptionPane.showMessageDialog(null, "Item is deleted from the database!");
textfield5.setText("");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
update.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
//guiFrame6.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame6.setTitle("Update Items");
guiFrame6.setSize(350,250);
//This will center the JFrame in the middle of the screen
guiFrame6.setLocationRelativeTo(null);
guiFrame6.setVisible(true);
ResultSet rs;
try {
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st2 = c.createStatement();
rs = st2.executeQuery("select itemname from stock");
JComboBox combo=new JComboBox();
panel6.add(label13);
panel6.add(combo);
panel6.add(label14);
panel6.add(textfield6);
panel6.add(label15);
panel6.add(textfield7);
panel6.add(save);
while (rs.next())
{
name = rs.getString("itemname");
combo.addItem(name);
}
combo.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
temp6=e.getItem().toString();
if (e.getStateChange() == e.SELECTED)
{
System.out.println(temp6 + " was selected\n");
//System.out.println(temp6);
save.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
try {
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st3 = c.createStatement();
System.out.println(temp6);
rate=textfield6.getText();
quantity=textfield7.getText();
String sql4 = "Update stock set rate='" + (rate)+ "', quantity='" + (quantity)+ "' where itemname = '" + (temp6) + "'";
st3.executeUpdate(sql4);
textfield6.setText("");
textfield7.setText("");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
}
});
guiFrame6.add(panel6);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
billing.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent event)
{
// guiFrame7.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame7.setTitle("Billing");
guiFrame7.setSize(500,200);
//This will center the JFrame in the middle of the screen
guiFrame7.setLocationRelativeTo(null);
guiFrame7.setVisible(true);
model.addColumn("ITEM NAME");
model.addColumn("RATE");
model.addColumn("QUANTITY");
model.addColumn("PRICE");
// Create a new table instance
table = new JTable( model );
// Add the table to a scrolling pane
scroll = new JScrollPane( table );
panel7.add( scroll, BorderLayout.CENTER );
panel7.add(addrow);
guiFrame7.add(panel7);
}
});
addrow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
ResultSet rs1;
try {
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st4 = c.createStatement();
rs1 = st4.executeQuery("select itemname from stock");
JComboBox combo=new JComboBox();
panel7.add(combo);
panel7.add(calculate);
panel7.add(newline1);
panel7.add(deleterow);
while (rs1.next())
{
name = rs1.getString("itemname");
combo.addItem(name);
}
combo.addItemListener(new ItemListener()
{
ResultSet rs4;
public void itemStateChanged(ItemEvent e)
{
temp6 = (String)e.getItem();
if (e.getStateChange() == e.SELECTED)
{
System.out.println(temp6 + " was selected\n");
try {
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st4 = c.createStatement();
rs4 = st4.executeQuery("select rate from stock where itemname = '"+(temp6)+"'");
while (rs4.next())
{
rate = rs4.getString("rate");
System.out.println(rate);
String[] adddata = { temp6, rate, "", "" };
model.addRow(adddata);
a=Integer.parseInt(rate);
b=model.getRowCount();
System.out.println(b);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
else
{
System.out.println(temp6 + " was deselected\n");
}
}
});
guiFrame6.add(panel6);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
calculate.addActionListener(new ActionListener() {
ResultSet rs5;
public void actionPerformed(ActionEvent event) {
try {
panel7.add(total);
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st5 = c.createStatement();
Statement st6= c.createStatement();
System.out.println(b);
System.out.println(i);
int su=0;
for(i=0;i<b;i++)
{
Object obj0 = GetData(table,i , p);
System.out.println("Cell value of 0 column and "+i+" row :" + obj0);
Object obj1 = GetData(table,i , j);
System.out.println("Cell value of 1 column and "+i+" row :" + obj1);
Object obj2 = GetData(table, i, k);
System.out.println("Cell value of 2 column and "+i+" row :" + obj2);
String s=obj0.toString();
int x=Integer.parseInt(obj1.toString());
int y=Integer.parseInt(obj2.toString());
rs5 = st5.executeQuery("select quantity from stock where itemname= '"+(s)+"'");
while (rs5.next())
{
quantity = rs5.getString("quantity");
System.out.println(quantity);
int change_quantity=Integer.parseInt(quantity);
if(change_quantity>=y)
{
int z=x*y;
su=su+z;
String sum_temp=Integer.toString(su);
amount.setText(sum_temp);
panel7.add(amount);
System.out.println(z);
Object obj3=z;
table.getModel().setValueAt(obj3, i, q);
int final_quantity=change_quantity-y;
System.out.println(final_quantity);
String sql5 = "Update stock set quantity='" + (final_quantity)+ "'where itemname = '" + (s) + "'";
st6.executeUpdate(sql5);
}
else
{
JOptionPane.showMessageDialog(null, "The quantity of the Item "+s+" is only "+change_quantity);
}
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
} });
viewstock.addActionListener(new ActionListener() {
ResultSet rs6;
public void actionPerformed(ActionEvent event) {
// guiFrame8.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guiFrame8.setTitle("View Stock");
guiFrame8.setSize(500,250);
//This will center the JFrame in the middle of the screen
guiFrame8.setLocationRelativeTo(null);
guiFrame8.setVisible(true);
//panel7.add(newline1);
model1.addColumn("ITEM CODE");
model1.addColumn("ITEM NAME");
model1.addColumn("RATE");
model1.addColumn("QUANTITY");
// Create a new table instance
table1 = new JTable( model1 );
// Add the table to a scrolling pane
scroll1 = new JScrollPane( table1 );
panel8.add( scroll1, BorderLayout.CENTER );
try {
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st7 = c.createStatement();
rs6 = st7.executeQuery("select * from stock where itemname ");
// a=e.getStateChange();
while (rs6.next())
{
temp7 = rs6.getString("itemcode");
temp8=rs6.getString("itemname");
temp9=rs6.getString("rate");
temp10=rs6.getString("quantity");
System.out.println(temp7);
String[] adddata = { temp7, temp8, temp9, temp10 };
model1.addRow(adddata);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
guiFrame8.add(panel8);
}
});
deleterow.addActionListener(new ActionListener() {
ResultSet rs7;
public void actionPerformed(ActionEvent event) {
int r=table.getSelectedRow();
int n=table.getRowCount();
System.out.println(r);
Object obj4 = GetData(table,r, p);
System.out.println("Cell value of 0 column and "+r+" row :" + obj4);
Object obj5 = GetData(table, r, k);
System.out.println("Cell value of 2 column and "+r+" row :" + obj5);
Object obj6 = GetData(table, r, k+1);
System.out.println("Cell value of 2 column and "+r+" row :" + obj6);
if(obj6!="")
{
int su=0;
int y=Integer.parseInt(obj5.toString());
int x=Integer.parseInt(obj6.toString());
String s=obj4.toString();
try
{
System.out.println("connecting to the " + name1 + " data source...");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url, name1, pass);
Statement st8 = c.createStatement();
Statement st9= c.createStatement();
rs7 = st8.executeQuery("select quantity from stock where itemname= '"+(s)+"'");
while (rs7.next())
{
quantity = rs7.getString("quantity");
System.out.println(quantity);
int change_quantity=Integer.parseInt(quantity);
int final_quantity=change_quantity+y;
System.out.println(final_quantity);
String sql5 = "Update stock set quantity='" + (final_quantity)+ "'where itemname = '" + (s) + "'";
st9.executeUpdate(sql5);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
model.removeRow(r);
for(i=0;i<n;i++)
{
Object obj2 = GetData(table, i, k+1);
System.out.println("Cell value of 2 column and "+i+" row :" + obj2);
int h=Integer.parseInt(obj2.toString());
su=su+h;
String sum_temp=Integer.toString(su);
amount.setText(sum_temp);
}
}
else
{
model.removeRow(r);
}
}
});
}
public Object GetData(JTable table, int row_index, int col_index)
{
return table.getModel().getValueAt(row_index, col_index);
}
public static void main(String[] args)
{
First f=new First();
}
}