Sunteți pe pagina 1din 26

Table Structure, Definition and Queries

Query to create and use the database


Mysql>create database if not exists test; Mysql>use test;

Query to create the table structure


Mysql>create table toys( toycode int(11) primary key not null, name varchar(50) not null, descp varchar(200), qtyinstock int(11), agegroulowerlimit int(11), agegroupupperlimit int(11), forgender char(10) not null default "A", price float );

Query to view the structure of the table


Mysql>Desc toys;

Queries for inserting records in the table


Mysql>insert into toys values(101, "Furreal Zambi", "Toy elephant which produces sound as like real elephant when you switch it on to make it walk. BEst for infants who love elephants.", 100, 2, 5, "A", 150); Mysql>insert into toys values(102, "Battle Pack", "Superhero game", 75, 5, 7, "B", 165); Mysql>insert into toys values(103, "Kidie Zoom", "Kid Camera", 150, 8, 10, "A", 355); Mysql>insert into toys values(104, "Robot Truck", "Vehicle Toy", 120, 4, 8, "B", 200); Mysql>insert into toys values(105, "Barbie doll", "Popular Doll", 300, 4, 10, "G", 225); Mysql>insert into toys values(106, "Rubik 360 Puzzle", "Puzzle Game", 95, 8, 13, "A", 400); Mysql>insert into toys values(107, "Talking de Li", "Doll", 150, 4, 10, "G", 215); Mysql>insert into toys values(108, "Go go Pets", "Interactive Animal Toy", 200, 4, 8, "A", 260); Mysql>insert into toys values(109, "Racing Car", "Electronic Toy car. Race, ride and do much with your friends with the range of these supercars.", 300, 3, 9, "B", 200); Mysql>insert into toys values(110, "Bendaroos Mega Pack", "Art & Craft Toy", 250, 6, 10, "G", 200); Mysql>insert into toys values(111, "Maple Tree", "Christmas tree decorated with lights. Enjoy this christamas with this holy tree", 125, 10, 18, "A", 430); Mysql>insert into toys values(112, "Medussa", "Greek goddess playable statue", 80, 6, 10, "G", 200); Mysql>insert into toys values(113, "hi-5", "Alarming gagdget using polyphonic alarming tones which says hello and hi-5 when you wake up.", 40, 8, 16, "A", 80); Mysql>insert into toys values(114, "Herald jingles", "Muscial toy with preloaded herald songs. Best for kids.", 420, 4, 10, "A", 200); Mysql>insert into toys values(115, "Chess player", "Chess Board", 280, 8, 18, "A", 150); Mysql>insert into toys values(116, "Riddles", "Solve riddles and test the power of your brain", 90, 6, 15, "A", 240);

// On clicking importing the packages required

import javax.swing.JOptionPane;
// On clicking Exit

Toys e-Shopping application main code

private void exitActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(rootPane, "<HTML>Thanks for using e-shopping Application"); System.exit(0); }
// code for e-shopping manager button

private void eShoppingActionPerformed(java.awt.event.ActionEvent evt) { a(); }


// code for Toys info button

private void infoActionPerformed(java.awt.event.ActionEvent evt) { b(); }


// code for e-shopping manager button

private void addNewActionPerformed(java.awt.event.ActionEvent evt) { d(); }


// code for About button

private void aboutActionPerformed(java.awt.event.ActionEvent evt) { c(); }

// Declaration of methods a, b & c

public void a(){ new es().setVisible(true); } public void b(){ new info().setVisible(true); } public void c(){ new About().setVisible(true); } public void d(){ new addNew().setVisible(true); }

e-Shopping Manager JFrame code


//code for importing packages written at the top of the source window

import javax.swing.DefaultListModel; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel;

//declaration of method for emptying the list

public void emptyList(){ DefaultListModel model=(DefaultListModel)toyList.getModel(); while(model.getSize()>0) model.remove(0); }


//Code for search by Price filter criteria

private void srchPriceRBActionPerformed(java.awt.event.ActionEvent evt) { if(srchPriceRB.isSelected()) { dispLabel1.setText("Enter Lower limit"); dispLabel2.setEnabled(true); upperTF.setEnabled(true); lowerTF.setEnabled(true); } }
//Code for search by Age filter criteria

private void srchAgeRBActionPerformed(java.awt.event.ActionEvent evt) { if (srchAgeRB.isSelected()) { dispLabel1.setText("Enter Age"); dispLabel2.setEnabled(false); upperTF.setEnabled(false); lowerTF.setEnabled(true); } else { dispLabel1.setText("Enter Lower Limit"); dispLabel2.setEnabled(true); upperTF.setEnabled(true); lowerTF.setEnabled(true); } }
6

searchPriceRB upperTF

searchAgeRB dispLabel2

searchNameRB

dispLabel1 tCodeLbl

lowerTF

searchBtn tageGrpLbl tNameLbl

toyList

tPriceLbl tQtyLbl OrdQtyTF confirmBtn

tDesLbl

buyCB

orderTbl
totAmtLbl

submitBtn

exitBtn

e-Shopping Manager JFrame code


//Code for search by Name filter criteria

private void srchNameRBActionPerformed(java.awt.event.ActionEvent evt) { if(srchNameRB.isSelected()) { dispLabel1.setText("Enter Name"); upperTF.setEnabled(false); dispLabel2.setEnabled(false); lowerTF.setEnabled(true); } else { dispLabel1.setText("Enter Lower limit"); dispLabel2.setEnabled(true); upperTF.setEnabled(false); lowerTF.setEnabled(true); } }

e-Shopping Manager JFrame code


//Code for Search Button

private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) { emptyList(); String filter=""; if (srchNameRB.isSelected()) { filter="WHERE name like '%"+lowerTF.getText()+"%' "; } else if(srchPriceRB.isSelected()) { float priceL=Float.parseFloat(lowerTF.getText()); float priceF=Float.parseFloat(upperTF.getText()); filter="WHERE price>="+priceL+" and price <="+priceF; } else if(srchAgeRB.isSelected()){ int age=Integer.parseInt(lowerTF.getText()); filter="WHERE agegroulowerlimit<= "+age+" and agegroupupperlimit >= "+age; } int count=0; String query="Select name from toys "+filter+" ;"; DefaultListModel model=(DefaultListModel)toyList.getModel(); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(query); while(rs.next()){ model.add(count,rs.getString("name")); count++; } rs.close(); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity"); } ordQtyTF.setEnabled(false); }
//Code for check box for confirming to buy the item or not

private void buyCBActionPerformed(java.awt.event.ActionEvent evt) { if(buyCB.isSelected()) { ordQtyTF.setEnabled(true); } else ordQtyTF.setEnabled(false); }


10

11

e-Shopping Manager JFrame code


//Code for List value changed event

private void toyListValueChanged(javax.swing.event.ListSelectionEvent evt) { String selval=(String)toyList.getSelectedValue(); String query="select * from toys where name like'%"+selval+"%';"; try{ Class.forName("java.sql.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(query); while(rs.next()){ tCodeLbl.setText(""+rs.getInt(1)); tNameLbl.setText(rs.getString(2)); tDesLbl.setText(rs.getString(3)); tQtyLbl.setText(""+rs.getInt(4)); tageGrpLbl.setText(""+rs.getInt(5)+"to"+rs.getInt(6)); tPriceLbl.setText(""+rs.getFloat(8)); } rs.close(); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity"); } }

//Code for finally confirming the transaction Button

private void submitBtnActionPerformed(java.awt.event.ActionEvent evt) { DefaultTableModel model=(DefaultTableModel)orderTbl.getModel(); int count=model.getRowCount(); String total=totAmtLbl.getText(); JOptionPane.showMessageDialog(rootPane, "You have ordered for "+count+" items \n and your bill amount is Rs."+total); }

12

13

e-Shopping Manager JFrame code


//Code for making the purchase ( confirm ) Button

private void confirmBtnActionPerformed(java.awt.event.ActionEvent evt) { int qtyA=Integer.parseInt(tQtyLbl.getText()); int qtyO=Integer.parseInt(ordQtyTF.getText()); if(qtyO>qtyA) { JOptionPane.showMessageDialog(rootPane, "Only "+qtyA+" items of "+tCodeLbl.getText()+" are in stock! \n SORRY, cannot take your order."); } else{ float amt=qtyO*Float.parseFloat(tPriceLbl.getText()); float total=Float.parseFloat(totAmtLbl.getText()); DefaultTableModel model=(DefaultTableModel)orderTbl.getModel(); int tc=Integer.parseInt(tCodeLbl.getText()); String qry="Update TOYS SET qtyinstock=qtyinstock-"+qtyO+" Where toycode ="+tc+" ;"; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); stmt.executeUpdate(qry); model.addRow(new Object[] { tCodeLbl.getText(),tNameLbl.getText(),tPriceLbl.getText(),ordQtyTF.getText(),amt }); total=total+amt; totAmtLbl.setText(""+total); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity"); } ordQtyTF.setText(""); ordQtyTF.setEnabled(false); buyCB.setSelected(false); } }

//Code for Cancel Button

private void exitBtnActionPerformed(java.awt.event.ActionEvent evt) { JOptionPane.showMessageDialog(rootPane, "Have a nice day"); dispose(); }

14

//for importing packages written on the top of the source window

import javax.swing.DefaultListModel; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import javax.swing.JOptionPane;

Toys Info jFrame source Code

//declaration of method for emptying the list

public void emptyList(){ DefaultListModel model=(DefaultListModel)tList.getModel(); while(model.getSize()>0) model.remove(0); }

//Code for Get List of available Toys Button

private void getdetailsActionPerformed(java.awt.event.ActionEvent evt) { emptyList(); int count=0; String query="Select name from toys ;"; DefaultListModel model=(DefaultListModel)tList.getModel(); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(query); while(rs.next()){ model.add(count,rs.getString("name")); count++; } rs.close(); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity \n sorry for incovenience"); } }

15

getdetails

tCodeLBl

agl tNameLbl

agh

tDesLbl

tQtyLbl

tPriceLbl tList edit updtrcrd tSexLbl deletercrd exit

16

//Code for List Item changed event of the toy list

Toys Info jFrame source Code

private void tListValueChanged(javax.swing.event.ListSelectionEvent evt) { String selval=(String)tList.getSelectedValue(); String query="select * from toys where name like'%"+selval+"%';"; try{ Class.forName("java.sql.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(query); while(rs.next()){ tCodeLbl.setText(""+rs.getInt(1)); tNameLbl.setText(rs.getString(2)); tDesLbl.append(rs.getString(3)); tQtyLbl.setText(""+rs.getInt(4)); agl.setText(""+rs.getInt(5)); agh.setText(""+rs.getInt(6)); tPriceLbl.setText(""+rs.getFloat(8)); tSexLbl.setText(""+rs.getString(7)); } rs.close(); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity \n sorry for incovenience."); } edit.setEnabled(true); deletercrd.setEnabled(true); }
//Code for Edit Button

private void editActionPerformed(java.awt.event.ActionEvent evt) { updtrcrd.setEnabled(true); tNameLbl.setEditable(true); tDesLbl.setEditable(true); tQtyLbl.setEditable(true); agh.setEditable(true); agl.setEditable(true); tPriceLbl.setEditable(true); tSexLbl.setEditable(true); }

17

18

Toys Info jFrame source Code


//Code for Delete Records Button

private void deletercrdActionPerformed(java.awt.event.ActionEvent evt) { int res=JOptionPane.showConfirmDialog(null,"Want to remove the toy for sure ?"); if(res==JOptionPane.YES_OPTION){ try{ Class.forName("java.sql.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); String queryd="Delete from toys where toycode="+tCodeLbl.getText()+";"; stmt.executeUpdate(queryd); JOptionPane.showMessageDialog(null,"Record Deleted !"); getdetails.doClick(); tCodeLbl.setText(""); tNameLbl.setText(""); tDesLbl.append(""); tQtyLbl.setText(""); agh.setText(""); agl.setText(""); tPriceLbl.setText(""); tSexLbl.setText(""); edit.setEnabled(false); updtrcrd.setEnabled(false); deletercrd.setEnabled(false); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity \n sorry for incovenience."); } } }

19

20

Toys Info jFrame source Code


//Code for Update Records Button

private void updtrcrdActionPerformed(java.awt.event.ActionEvent evt) { String qury="Update toys set name= '"+tNameLbl.getText()+"',descp= '"+tDesLbl.getText()+"',qtyinstock= "+tQtyLbl.getText()+" ,agegroulowerlimit= "+agl.getText()+" ,agegroupupperlimit= "+agh.getText()+" ,forgender= '"+tSexLbl.getText()+"',price= "+tPriceLbl.getText()+"where toycode="+tCodeLbl.getText()+";"; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); stmt.executeUpdate(qury); JOptionPane.showMessageDialog(null,"Successfully Updated"); tNameLbl.setEditable(false); tDesLbl.setEditable(false); tQtyLbl.setEditable(false); agh.setEditable(false); agl.setEditable(false); tPriceLbl.setEditable(false); tSexLbl.setEditable(false); updtrcrd.setEnabled(false); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity \n sorry for incovenience"); } }

//Code for Cancel Button

private void exitActionPerformed(java.awt.event.ActionEvent evt) { dispose(); }

21

//for importing packages written on the top of the source window

Code for Add New jFrame source code

import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.ResultSet; import javax.swing.JOptionPane;

//code for fetch button private void fetchActionPerformed(java.awt.event.ActionEvent evt) { String query="Select toycode from toys;"; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery(query); while(rs.next()){ codet.setText(""+rs.getInt(1)); } rs.close(); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity \n sorry for incovenience"); } int a=Integer.parseInt(codet.getText()); int b=++a; codet.setText(""+b); }

22

codet

namet

fetch dest

prct

lowt hight gent qtyt addnew exit

23

//code for Add new Button private void addnewActionPerformed(java.awt.event.ActionEvent evt) { String qury="Insert into toys values ( "+codet.getText()+" , '"+namet.getText()+"', '"+dest.getText()+"', "+qtyt.getText()+" , "+lowt.getText()+" , "+hight.getText()+" , '"+gent.getSelectedItem()+"', "+prct.getText()+" );"; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","inspire11"); Statement stmt=con.createStatement(); stmt.executeUpdate(qury); JOptionPane.showMessageDialog(null,"Successfully Added"); fetch.doClick(); namet.setText(""); dest.setText(""); prct.setText(""); lowt.setText(""); hight.setText(""); namet.setText(""); qtyt.setText(""); stmt.close(); con.close(); } catch(Exception e){ JOptionPane.showMessageDialog(null,"Error in connectivity \n sorry for incovenience"); } }

Code for Add New jFrame source code

// code for cancel button private void exitActionPerformed(java.awt.event.ActionEvent evt) { dispose(); }

24

//Code for Cancel Button

private void exitActionPerformed(java.awt.event.ActionEvent evt) { dispose(); }

25

Bibliography
Reference of the information used here 1. Informatics Practices for class XII Sumita Arora

26

S-ar putea să vă placă și