Upload
rakesh-roushan
View
219
Download
0
Embed Size (px)
Citation preview
8/3/2019 Main Print
1/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;
1
8/3/2019 Main Print
2/26
2
8/3/2019 Main Print
3/26
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);
3
8/3/2019 Main Print
4/26
4
8/3/2019 Main Print
5/26
Toys e-Shopping application main code
// On clicking importing the packages required
import javax.swing.JOptionPane;
// On clicking Exit
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane,"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);
}
5
8/3/2019 Main Print
6/26
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
8/3/2019 Main Print
7/26
7
searchPriceRB searchAgeRBsearchNameRB
searchBtndispLabel1
upperTF dispLabel2
lowerTF
oyList
tCodeLbltageGrpLbl
tNameLbl
tDesLbltPriceLbl
tQtyLbluyCB
OrdQtyTF
confirmBtn
orderTbl
submitBtnexitBtn
totAmtLbl
8/3/2019 Main Print
8/26
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);
}
}
8
8/3/2019 Main Print
9/26
9
8/3/2019 Main Print
10/26
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
8/3/2019 Main Print
11/26
11
8/3/2019 Main Print
12/26
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
8/3/2019 Main Print
13/26
13
8/3/2019 Main Print
14/26
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
8/3/2019 Main Print
15/26
Toys Info jFrame source Code
//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;
//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 voidgetdetailsActionPerformed(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
8/3/2019 Main Print
16/26
16
getdetails
tList
exit
deletercrdupdtrcrd
edit
tCodeLBlagl agh
tNameLbl
tDesLbl
tPriceLbl
tSexLbl
tQtyLbl
8/3/2019 Main Print
17/26
Toys Info jFrame source Code
//Code for List Item changed event of the toy list
private voidtListValueChanged(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
8/3/2019 Main Print
18/26
18
8/3/2019 Main Print
19/26
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
8/3/2019 Main Print
20/26
20
8/3/2019 Main Print
21/26
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 voidexitActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
21
8/3/2019 Main Print
22/26
Code for Add New jFrame source code
//for importing packages written on the top of the source window
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.Statement;
importjava.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
8/3/2019 Main Print
23/26
23
fetch
codetnamet
dest
prct
lowt
hight
gent
qtyt
addnew exit
8/3/2019 Main Print
24/26
Code for Add New jFrame source code
//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 cancel button
private void exitActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
24
8/3/2019 Main Print
25/26
//Code for Cancel Button
private voidexitActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
25
8/3/2019 Main Print
26/26
Bibliography
Reference of the information used here -
1. Informatics Practices for class XII Sumita Arora