JAVA AND MYSQL QUERIES

55
1 Class XII - Informatics Practices (065) P R A C T I C A L – A S S I G N M E N T S PART 1 JAVA Experiment No. 1: Objective: Understanding and use of variables of float and other data types. Task: Develop a simple Calculator application as per given screen snapshot, toImplement +, -, x and / operations. The text boxes get cleared when ‘C’ buttonis clicked. Public Class :- Float n1,n2,res; char op; 1.CLEAR Button:- t1.setText(null);

Transcript of JAVA AND MYSQL QUERIES

Page 1: JAVA AND MYSQL QUERIES

1

Class XII - Informatics Practices (065)

P R A C T I C A L – A S S I G N M E N T S

PART 1 JAVA

Experiment No. 1:Objective: Understanding and use of variables of float and other data types.Task: Develop a simple Calculator application as per given screen snapshot, toImplement +, -, x and / operations. The text boxes get cleared when ‘C’ buttonis clicked.

Public Class :-Float n1,n2,res;

char op;

1.CLEAR Button:-

t1.setText(null);

t2.setText(null);

t3.setText(null);

t3.setText(null);

Page 2: JAVA AND MYSQL QUERIES

2

2.EXIT Button:-

System.exit(0);

3.+ Button:-

n1=Float.parseFloat(t1.getText());

n2=Float.parseFloat(t2.getText());

res=n1+n2;

t3.setText(""+res);

4.- Button:-

n1=Float.parseFloat(t1.getText());

n2=Float.parseFloat(t2.getText());

res=n1-n2;

t3.setText(""+res);

5. *Button:-

n1=Float.parseFloat(t1.getText());

n2=Float.parseFloat(t2.getText());

res=n1*n2;

t3.setText(""+res);

6. / Button:-

n1=Float.parseFloat(t1.getText());

n2=Float.parseFloat(t2.getText());

res=n1/n2;

t3.setText(""+res);

Page 3: JAVA AND MYSQL QUERIES

3

Experiment No. 2:

Objective: Understanding the real life application requirement and developing a solution.Task: Develop a Compound Interest Calculator application as per given screensnapshot, to calculate total amount for given Amount, Rate of Interest andTime using (A=P(1+R/100) T) and Interest I=A-P.

Public Class :-float p,r,n,res,intamt;

1. CALCULATE BUTTON :- p=Float.parseFloat(txtPA.getText()); r=Float.parseFloat(txtRate.getText()); n=Float.parseFloat(txtDur.getText()); res=(p*r*n)/100; intamt=p-res; txtSI.setText(""+res); Tfintamt.setText(""+intamt);

2. Clear Button :- txtPA.setText(""); txtRate.setText(""); txtDur.setText(""); txtSI.setText("");

Page 4: JAVA AND MYSQL QUERIES

4

3. EXIT Button System.exit(0)

Experiment No. 3:

Objective: Understanding and use of Nested conditions in the Real life applications.Task: A Quick Fox Transport Co. wants to develop an application for calculatingAmount based on distance and weight of goods.The charges (Amount) to be calculated as per rates given below.

Distance Weight Charges per Km.>=500 Km >=100 kg. Rs. 5/-

>=10 and <100 kg. Rs. 6/-< 10 kg. Rs. 7/-

<500 Km >=100 Kg. Rs.8/-<100 Kg. Rs.5/-

Public Class :-int w,d,a;

1. Calculate Button :-t1.getText(); t2.getText(); w=Integer.parseInt(t3.getText());

Page 5: JAVA AND MYSQL QUERIES

5

d=Integer.parseInt(t4.getText()); if(d>=500 && w>=100) { a=d*5; t5.setText(""+a); } else if(d>=500 && w<100 && w>=10) { a=d*6; t5.setText(""+a); } else if(d<=500 && w<10) { a=d*7; t5.setText(""+a); } else if(d<500 && w>=100) { a=d*8; t5.setText(""+a); } else if(d<500 && w<100) { a=d*5; t5.setText(""+a); }

2. Exit Button :-System.exit(0);

Page 6: JAVA AND MYSQL QUERIES

6

Experiment No. 4:

Objective: Understanding and use of Nested loops and Text Area control.Task: Develop a Java application to print a Pattern for given character and steps, asper given screen shot.

Public Class :-char s;String s1;int n;

1. GENERATE BUTTON :- s1=t1.getText(); s=s1.charAt(0); n=Integer.parseInt(t2.getText()); for(int i=1;i<=n;i++) { for(int j=1;j<=i;j++) { ta1.append(""+s+"\n"); } }

2. CLEAR BUTTON :-t1.setText(null);t2.setText(null);ta1.setText(null);

Page 7: JAVA AND MYSQL QUERIES

7

Experiment No. 5:

Objective: Understanding the use of loops and mathematical operations.Task: Develop an application to compute the sum of digits for given number.

Public Class :-int no,sum,r;

1.Calculate it Button :-r=0;no=Integer.parseInt(t1.getText());while(no>0){r=(no%10)+(r*10);sum=(no%10)+ sum;no/=10;}ts.setText(""+sum);

2.Clear Button :-t1.setText("");ts.setText("");

3.Exit Button :- System.exit(0);

Page 8: JAVA AND MYSQL QUERIES

8

Experiment No. 6:

Objective:Understanding and developing logic to solve a problem.Use of nested loopsand breaking it prematurely.Task: Develop a Prime Number Generator Application which generates Prime numbersfor given range. Prime numbers are those numbers which are divisible by one or itself only.

Public Class :-int l,h,r,f;

1. Generate Button :-l=Integer.parseInt(t1.getText());h=Integer.parseInt(t2.getText());for(r=l;r<=h;r++){f=0;for(int j=2;j<r/2;j++){ if(r%j==0){f=1;break;} }if(f==0){ta1.append(""+r+"\n");}

Exit Button :- System.exit(0);

Page 9: JAVA AND MYSQL QUERIES

9

Experiment No. 7:Objective: Displaying images on a Label and Text Area control.Task: Develop an e-Learning application with images and text information as pergiven screen shot.

1. Keyboard

pic.setIcon(new ImageIcon("C:\\Documents and Settings\\Student\\My Documents\\NetBeansProjects\\offer_of_the_day\\applewireless.png"));

ta.setText("Offer Price:Rs 5,400/-.\n"+

"Maximum Retail Price:Rs 6,700/-.\n"+

" Set the keyboard on your desk, and you’ll see that it takes up far less space — up to 24 percent less — than other full-size keyboards. And you have even more room to mouse comfortably alongside of it. You’ll also feel a difference when you type. Your hands will appreciate the low-profile aluminium enclosure and the crisp, responsive keys. Use the new wireless Magic Mouse alongside the Apple Wireless Keyboard and get the full effect of a cable-free (and clutter-free)

Page 10: JAVA AND MYSQL QUERIES

10

desk.Use the function keys for one-touch access to a variety of Mac features including Mission Control and Launchpad.");

2.Mouse

pic.setIcon(new ImageIcon("C:\\Documents and Settings\\Student\\My Documents\\NetBeansProjects\\offer_of_the_day\\mouse.png"));

ta.setText("Offer Price: Rs 3,200/-.\n"+"Maximum Retail Price:Rs 3,900/-.\n"+

"The Multi-Touch area covers the top surface of Magic Mouse, and the mouse itself is the button. Scroll in any direction with one finger, swipe through web pages and photos with two, and click and double-click anywhere. Inside Magic Mouse is a chip that tells it exactly what you want to do. Which means Magic Mouse won’t confuse a scroll with a swipe. It even knows when you’re just resting your hand on it.Laser-Tracking EngineMagic Mouse uses powerful laser tracking that’s far more sensitive and responsive on more surfaces than traditional optical tracking. That means it tracks with precision on nearly every surface —.");

3.Monitor

pic.setIcon(new ImageIcon("C:\\Documents and Settings\\Student\\My Documents\\NetBeansProjects\\offer_of_the_day\\monitor.png"));

ta.setText("Offer Price: Rs 69,999/-.\n"+"Maximum Retail Price: Rs 1,09,999/-\n"+

"LED backlighting. Bright on.Full brightness with no waiting. That’s the big advantage of LED backlight technology. Unlike most displays that take time to warm up before they reach maximum brightness, an LED-backlit display is instantly on and uniformly bright. LED backlighting also gives you greater control over screen brightness. So you can tune the Thunderbolt Display to suit the ambient light in even the dimmest room.");

4.Printer

pic.setIcon(new ImageIcon("C:\\Documents and Settings\\Student\\My Documents\\NetBeansProjects\\offer_of_the_day\\printer.png"));

Page 11: JAVA AND MYSQL QUERIES

11

ta.setText("Offer Price: Rs 16,750/-\n"+"Maximum Retail Price:Rs 19,650/-\n"+

"The PIXMA Printing Solutions App available on both iOS and Android platforms, gives users a lot more functionality with their printers. Using a smartphone or tablet device, users can print documents and images stored on their devices, or scan a document directly into their devices.");

5.Modem

pic.setIcon(new ImageIcon("C:\\Documents and Settings\\Student\\Desktop\\exp7\\modem.png"));

ta.setText("Offer Price:Rs 3,600/-\n"+"Maximum Retail Price:Rs 4,000/-\n"+

"Enjoy fast wireless connectivity for your home or office. The Wireless-N Home ADSL2 Modem Router has a built-in high-speed ADSL2 Modem for a fast, \"Always On\" connection to the Internet. Wireless connections use Wireless-N technology for fast data transfers, and the four built-in 10/100 Ethernet ports supply high-speed connections to your wired devices. ");

Page 12: JAVA AND MYSQL QUERIES

12

Experiment No. 8:Objective:Demonstration of use of List Dynamically through code.Task: Develop an application as per given screen shot to Add , Remove the givenmembers of list and display the selected item in a text field using List control.

Public Class :-

String a,r,s;

1. Add Button :-DefaultListModel dd=(DefaultListModel) li1.getModel();a=t2.getText();dd.addElement(a);t2.setText("");

2. Remove Button :-int s;DefaultListModel d=(DefaultListModel) li1.getModel(); s=Integer.parseInt(t3.getText());d.removeElementAt(s);

3. Clear List Button :-DefaultListModel d=(DefaultListModel) li1.getModel();dd.removeAllElements();

4. List Value changed :-t1.setText(li1.getSelectedValue().toString());

Page 13: JAVA AND MYSQL QUERIES

13

Experiment No. 9:Objective:Understanding and use of the Radio Button in Real-life application to determine the selection of choice and calculations accordingly.Task:Develop a Billing application for Happy Shopping- A retail chain involved in sales of readymade garments. The happy Shopping offers discount to its members holding Platinum, Gold and Silver card.The 10% discount is given to Platinum card, 8% to Gold Card and 5% to SilverCard holders on sales amount.

Public Class :-

float r,q,ta,d,na;

1. Calculate Button :- r=Float.parseFloat(t1.getText()); q=Float.parseFloat(t2.getText()); if(r1.isSelected()==true) { ta=r*q; t3.setText(""+ta); d=ta*10/100; t4.setText(""+d);

Page 14: JAVA AND MYSQL QUERIES

14

na=ta-d; t5.setText(""+na); } if(r2.isSelected()==true) { ta=r*q; t3.setText(""+ta); d=ta*8/100; t4.setText(""+d); na=ta-d; t5.setText(""+na); } if(r3.isSelected()==true) { ta=r*q; t3.setText(""+ta); d=ta*5/100; t4.setText(""+d); na=ta-d; t5.setText(""+na); }

2. Clear Button :- tName.setText(null); t1.setText(null); t2.setText(null); t3.setText(null); t4.setText(null); t5.setText(null); r1.setSelected(false); r2.setSelected(false); r3.setSelected(false);

3. R1 Radio Button :- if(r1.isSelected()==true) r2.setSelected(false); r3.setSelected(false);

Page 15: JAVA AND MYSQL QUERIES

15

4. R2 Radio Button :- if(r2.isSelected()==true) { r1.setSelected(false); r3.setSelected(false); }

5. R3 Radio Buttton :- if(r3.isSelected()==true) { r1.setSelected(false); r2.setSelected(false); }

6. Exit Button :- System.exit(0);

Page 16: JAVA AND MYSQL QUERIES

16

Experiment No. 10:Task: The Entertainment Paradise- A theater in Delhi wants to develop a computerized Booking System. The proposed Interface is given below. The theater offers different types of seats. The Ticket rates are-

Stalls - Rs. 625/- Circle - Rs.750/- Upper Class - Rs.850/Box - Rs.1000/-

A discount is given 10% of total amount if tickets are purchased on Cash. In case of credit card holders 5% discount is given.

Public Class :-int ta,d,na,ts;

1. Calculate Button :-ts=Integer.parseInt(tf1.getText());if(rstalls.isSelected()==true){if(rcash.isSelected()==true);{ta=625*ts;d=ta*10/100; na=ta+d;t1.setText(""+ta);

Page 17: JAVA AND MYSQL QUERIES

17

t2.setText(""+d);t3.setText(""+na);}if(racredit.isSelected()==true){ta=625*ts;d=ta*5/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na);}}if(rcircle.isSelected()==true){if(rcash.isSelected()==true);{ta=750*ts;d=ta*10/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na);}if(rcredit.isSelected()==true){ta=750*ts;d=ta*5/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na); }}if(rupperclass.isSelected()==true){if(rcash.isSelected()==true);{

Page 18: JAVA AND MYSQL QUERIES

18

ta=900*ts;d=ta*10/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na);}if(rcredit.isSelected()==true){ta=900*ts;d=ta*5/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na); }}if(rbox.isSelected()==true){if(rcash.isSelected()==true);{ta=1050*ts;d=ta*10/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na);}if(rcredit.isSelected()==true){ta=1050*ts;d=ta*5/100; na=ta+d;t1.setText(""+ta);t2.setText(""+d);t3.setText(""+na); }}

Page 19: JAVA AND MYSQL QUERIES

19

2. Exit Button :-System.exit(0);Experiment No. 11:

Objective:Understanding the use of various controls in Real life application.Task: ABC Consultancy is a placement organization, assists job seekers. The Entry form has to be designed to facilitate the Registration Process with following features.1. When Submit button is pressed, the following things should happen.(a) If Post Graduate is checked, the 10+2 and Graduate

checkboxes should also get selected automatically.(b) If Graduate is checked, the 10+2 checkboxes should also get

selected.(c) A Message Box with “Hello Mr. ….. You are registered” or

“Hello Miss… ….. You are registered” as per Gender of candidate.

2. When Clear Button is pressed, all the text boxes, check boxes get cleared, and Male and Science option is selected by default.

Public Class :-

String str;

1. Submit Button :-if (pgra.isSelected())

Page 20: JAVA AND MYSQL QUERIES

20

{ gra.setEnabled(true); ten.setEnabled(true);}if (gra.isSelected()== true){ ten.setEnabled(true);} String str=null; if (mr.isSelected()) { str="hello Mr." + t1.getText()+ "you are registered."; } else if (fm.isSelected()){ str="Hello Miss"+t1.getText()+"you are registered"; } JOptionPane.showMessageDialog(null, str);

2. Clear Button :-ten.setSelected(false);gra.setSelected(false);pgra.setSelected(false);t1.setText(null);mr.setSelected(false);fm.setSelected(false);sci.setSelected(false);Comm.setSelected(false); art.setSelected(false);

3. Male Radio :-if(ramale.isSelected()==true){

rafemale.setSelected(false);}

4. Female Radio :-if(rafemale.isSelected()==true){

Page 21: JAVA AND MYSQL QUERIES

21

ramale.setSelected(false);}

5. 10+2 Checkbox :-

6. Graduate Check Box :-if(cbgrad.isSelected()==true){cb10.setSelected(true);}

7. Post Graduate Check Box :-if(cbpg.isSelected()==true){cb10.setSelected(true);cbgrad.setSelected(true)}

Page 22: JAVA AND MYSQL QUERIES

22

Experiment No. 12:Objective:Understanding the need of Real life applications.Task: The Milton Casting Company has developed an application to calculate the wage of its workers. The following functionalities are expected.1. The Wage rate are Rs.150/- (per day) for male and Rs.130/- for

females.2. An additional amount Rs.50/- per day is paid if worker is skilled.3. When Calculate Button is clicked the Total wage amount is

calculated and is played in relevant Text box.4. When Clear Button is clicked, all the text boxes get cleared and

Male option is selected.

Public Class :-

int d,ta;

1. Calculate Button :-

d=Integer.parseInt(t2.getText());

if(ch1.isSelected()==true)

Page 23: JAVA AND MYSQL QUERIES

23

{

if(ra1.isSelected()==true)

{

ra2.setSelected(false);

ta=d*200;

t3.setText(""+ta);

}

if(ra2.isSelected()==true)

{

ra1.setSelected(false);

ta=d*180;

t3.setText(""+ta);

}

}

if(ra1.isSelected()==true)

{

ra2.setSelected(false);

ta=d*150;

t3.setText(""+ta);

}

if(ra2.isSelected()==true)

{

ra1.setSelected(false);

ta=d*130;

Page 24: JAVA AND MYSQL QUERIES

24

t3.setText(""+ta);

}

2. Clear Button :-t1.setText(null);t2.setText(null);t3.setText(null);ra1.setSelected(false);ra2.setSelected(false);ch1.setSelected(false);

3. Exit Button :-System.exit(0);

Page 25: JAVA AND MYSQL QUERIES

25

Experiment No. 13:Objective:Understanding the need of Real life applications.Task:

The Fashion Gallery- a leading garments shop wants to develop an application tocalculate the discount Amount. The following functionalities are expected.1. The discount is given on the basis on payment mode.

Cash – 10%Cheque – 8% Credit – 5%

If Bill amount is more than 10000 then additional 5% discount is also given.

2. Initially, Calculate Net Amount is disabled, but when user click on Calculate Discount button the discount amount is calculated and displayed and Calculate Net Amount Button is enabled.

3. When Calculate Net Amount is clicked the Net Amount is calculated and displayed in Net Amount Text Box.

4. When Exit Button is clicked, a Confirm dialog appears and application is closed only when Yes option in confirm dialog is selected.

Public Class :-

Page 26: JAVA AND MYSQL QUERIES

26

float ba,d,na;

String a;

1.Calculate Discount Button :-

ba=Float.parseFloat(t2.getText());

if(ba>10000)

{

if(c1.getSelectedItem().equals("Cash"))

{

d=ba*15/100;

t3.setText(""+d);

}

if(c1.getSelectedItem().equals("Cheque"))

{

d=ba*13/100;

t3.setText(""+d);

b2.setEnabled(false);

}

if(c1.getSelectedItem().equals("Credit Card"))

{

d=ba*8/100;

t3.setText(""+d);

b2.setEnabled(false);

}

b2.setEnabled(true);

Page 27: JAVA AND MYSQL QUERIES

27

}

else

{

if(c1.getSelectedItem().equals("Cash"))

{

d=ba*10/100;

t3.setText(""+d);

}

if(c1.getSelectedItem().equals("Cheque"))

{

d=ba*8/100;

t3.setText(""+d);

b2.setEnabled(false);

}

if(c1.getSelectedItem().equals("Credit Card"))

{

d=ba*5/100;

t3.setText(""+d);

b2.setEnabled(false);

}

}

b2.setEnabled(true);

}

1. Calculate Net Amount Button :-

Page 28: JAVA AND MYSQL QUERIES

28

na=ba-d;

t4.setText(""+na);

2. Combo Box :-if(c1.getSelectedItem().equals("Cheque")){t3.setText(null);t4.setText(null);b2.setEnabled(false);}if(c1.getSelectedItem().equals("Credit Card")){t3.setText(null);t4.setText(null);b2.setEnabled(false);}

3. Exit Button :-System.exit(0);

Page 29: JAVA AND MYSQL QUERIES

29

Experiment No. 14:Objective:Understanding the use of String library methods.Task: Develop an application to generate the jumbled string for given string. The jumbled string is obtained by converting each alternate character in uppercase.

Public Class :-

Global variable declarations :-

String n1,n2;

1. Convert Button :-n1=t1.getText();n2=n1.toUpperCase();t2.setText(""+n2);

2. Exit Button :-Systemexit(0);

Page 30: JAVA AND MYSQL QUERIES

30

Experiment No. 15:Task: Develop an application to analyze the given string by counting the number ofvowels, consonants, digits and spaces etc.

Public Class :-

String str;int vowels=0,digits=0,space=0,consonent=0;char ch;

1. Check Button :-str=t1.getText();for(int i = 0; i < str.length(); i ++){ch = str.charAt(i);

if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')vowels ++;else if(Character.isDigit(ch))digits ++;else if(Character.isSpace(ch))space ++;else consonent++;

Page 31: JAVA AND MYSQL QUERIES

31

t2.setText(""+vowels);t3.setText(""+consonent);t4.setText(""+space);t5.setText(""+digits);}

2. Exit Button :- System.exit(0);

PART 2 JAVA APPLICATION & DATABASE CONNECTIVITY

Experiment No. 1:

Objective:Understanding the Database Handling in JAVA application.Task: Design a Java application to display the records of the Emp table of MYORGdatabase designed in the MySQL. The Structure of Emp table is as follows.

1. When Display record button is pressed, all the records are displayed.2. When Calculate Total Pay button is pressed, the total pay is calculatedand displayed in the Total Pay text box.

EName Char(40)Designation Char(30)Pay

Decimal(9,2)

Page 32: JAVA AND MYSQL QUERIES

32

Public Class :-Connection cn=null;Statement cmd=null;ResultSet rs=null;

Connection :-void est_connection(){ try { Class.forName("com.mysql.jdbc.Driver");

Page 33: JAVA AND MYSQL QUERIES

33

Connection cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/pk","root", "MKGM"); cmd=cn.createStatement(); } catch(Exception e) { JOptionPane.showMessageDialog(null,e.toString()); }}

1. Display Record :-DefaultTableModel dtv=(DefaultTableModel) vtable.getModel(); try { est_connection(); rs=cmd.executeQuery("select * from empdetails"); while(rs.next()) { dtv.addRow(new Object[]{rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),}); } } catch(Exception e) { JOptionPane.showMessageDialog(null,e.toString()); } }

2. Calculate Total Pay Button :- { try {

Page 34: JAVA AND MYSQL QUERIES

34

est_connection(); rs=cmd.executeQuery("select sum(pay)from empdetails"); if(rs.next()) { t1.setText(""+rs.getInt(1)); } } catch(Exception e) { JOptionPane.showMessageDialog(null, e.toString()); } }

3. Exit Button :-

System.exit(0);

Experiment No. 2:

Task: Design a fully featured Java application to navigate the records of the Emp table of MYORG database designed in the MySQL.

1. The First record is displayed when application is executed.2. Records are displayed in Text boxes as per Navigation

Buttons like First, Next etc are clicked.3. Initially, Save and Cancel Button is disabled. When Add

button is clicked, the Save and Cancel button is enabled and all the text boxes are cleared. The record is saved when Save button is pressed.

4. When Delete Button is clicked, the current record is deleted after getting confirmation from the user in Confirm dialog box.

5. When Modify button is clicked, user can edit the record. The modified record is saved when Save button is pressed.

Page 35: JAVA AND MYSQL QUERIES

35

Public ClassConnection cn=null;

Statement cmd=null;

ResultSet rs=null;

1. Add Button :-try { est_connection();cmd.executeUpdate("insert into empdetails values ('"+t1.getText() + "','" + t2.getText() + "','" + t3.getText() + "','" + t4.getText() + "');");JOptionPane.showMessageDialog(null,"Data inserted Successfully...");

Page 36: JAVA AND MYSQL QUERIES

36

} catch(Exception e) { JOptionPane.showMessageDialog(null,e.toString()); }

2. Delete Button :-ResultSet rs1; try { est_connection(); cmd.executeUpdate("delete from empdetails where name='"+t1.getText() +"';"); JOptionPane.showMessageDialog(null,"Data deleted Successfully..."); } catch(Exception e) { JOptionPane.showMessageDialog(null,e.toString()); }

3. Modify Button :-try { est_connection(); {cmd.executeUpdate("update empdetails set Designation='" + t2.getText() + "',pay='" + t3.getText() + "',city='" + t4.getText() + " where Name='" + t1.getText() + "');");JOptionPane.showMessageDialog(null,"Data updated Successfully..."); } } catch(Exception e) { JOptionPane.showMessageDialog(null,e.toString()); }

Page 37: JAVA AND MYSQL QUERIES

37

PART 3 MY SQL

Experiment No. 1:

Objective:Understanding the Database Handling through MySQL.Task:Create the following table and Answer the queries:

Table Name :Client_master

Name Type Attributes ---------------- --------------- ------------------- CLIENT_NO VARCHAR2(6) Primary Key NAME VARCHAR2(20) Not Null ADDRESS1 VARCHAR2(30)

Page 38: JAVA AND MYSQL QUERIES

38

ADDRESS2 VARCHAR2(30) CITY VARCHAR2(15) STATE VARCHAR2(15) PINCODE NUMBER(6) BAL_DUE NUMBER(10,2)

Data for client_master table

CLIENTNO.

CLIENT NAME CITY PINCODE STATE BAL_DUE

C00001 Ivan Bayross Bombay 400054 Maharashtra 15000

C00002 VandanaSaitwal Madras 780001 Tamil Nadu 0

C00003 PramadaJaguste Bombay 400057 Maharashtra 5000C00004 BasuNavindgi Bombay 400056 Maharashtra 0C00005 RaviShreedharan Delhi 100001 2000C00006 Rukmini Bombay 400050 Maharashtra 0

Table Name :Product_Master

Name Type Attributes --------------- ------ ------------------ PRODUCT_NO VARCHAR2(6) Primary Key DESCRIPTION VARCHAR2(15) Not Null PROFIT_PERCENT NUMBER(4,2) Not Null UNIT_MEASURE VARCHAR2(10) Not Null QTY_ON_HAND NUMBER(8) Not Null REORDER_LVL NUMBER(8) Not Null SELL_PRICE NUMBER(8,2) Not Null COST_PRICE NUMBER(8,2) Not Null

PRO.NO DESCRIPTION PROPER UNIT QTY REORDER_LVL SELLPRICE COSTP00001 1.44 Floppies 5 Piece 100 20 525 500P03453 Monitors 6 Piece 10 3 12000 11280P06734 Mouse 5 Piece 20 5 1050 1000P07865 1.22 Floppies 5 Piece 100 20 525 500P07868 Keyboards 2 Piece 10 3 3150 3050P07885 CD Drive 2.5 Piece 10 3 5250 5100P07965 540 HDD 4 Piece 10 3 8400 8000

Page 39: JAVA AND MYSQL QUERIES

39

P07975 1.44 Drive 5 Piece 10 3 1050 1000P08865 1.22 Drive 5 Piece 2 3 1050 1000

Table Name :Salesman_master Name Type Attributes ---------------- ---------- ------------- SALESMAN_NO VARCHAR2(6) Primary Key SALESMAN_NAME VARCHAR2(20) Not Null ADDRESS1 VARCHAR2(30) Not Null ADDRESS2 VARCHAR2(30) CITY VARCHAR2(20) PINCODE VARCHAR2(6) STATE VARCHAR2(20) SAL_AMT NUMBER(8,2) Not Null TGT_TO_GET NUMBER(6,2) Not Null YTD_SALES NUMBER(6,2) Not Null REMARKS VARCHAR2(60)

Data for Salesman Master:

NO Name Address1 Address2 City Pincode State Amt Target Sales RemarksS00001 Kiran A/14 Worli Bombay 400002 MAH 3000 100 50 Good S00002 Manish 65 Nariman Bombay 400001 MAH 3000 200 100 Good S00003 Ravi P-7 Bandra Bombay 400032 MAH 3000 200 100 Good S00004 Ashish A/5 Juhu Bombay 400044 MAH 3500 200 150 Good

Table Name :sales_order

Name Type Attributes --------------------- ---------- ------------- S_ORDER_NO VARCHAR2(6) Primary S_ORDER_DATE DATE CLIENT_NO VARCHAR2(6) Foreign Key client_no(client_master) DELY_ADDR VARCHAR2(25) SALESMAN_NO VARCHAR2(6) Foreign Key salesman_no(salesman_master) DELY_TYPE CHAR(1)BILLED_YN CHAR(1) DELY_DATE DATEORDER_STATUS VARCHAR2(10)

Page 40: JAVA AND MYSQL QUERIES

40

Data for Sales Order:SONO SODATE CLIENTNO DELY_TYPE BILL Y/N SALESMAN DELY_DATE ORDER_STATUS------------ ---------------------------- ---------------- -------------- ----------------- ------------------ ------------------------O19001 12-JAN-96 C00001 F N S00001 20-JAN-96 In Process O19002 25-JAN-96 C00002 P N S00002 27-JAN-96 Canceled O46865 18-FEB-96 C00003 F Y S00003 20-FEB-96 Fulfilled O19003 03-APR-96 C00001 F Y S00001 07-APR-96 Fulfilled O46866 20-MAY-96 C00004 P N S00002 22-MAY-96 Canceled O10008 24-MAY-96 C00005 F N S00004 26-MAY-96 In Process

Table Name :sales_order_details

Name Type Attributes ------------------ ------ ----------- S_ORDER_NO VARCHAR2(6) Primary Key PRODUCT_NO VARCHAR2(6) Primary Key / Foreign Key references

. QTY_ORDERED NUMBER(8) Product_no of product_master table QTY_DISP NUMBER(8) PRODUCT_RATE NUMBER(10,2)

Table Name :challan_headerName Type Attributes------------------ --------- -------------CHALLAN_NO VARCHAR2(6) Primary Key / first two letters mustStart with ‘CH’.S_ORDER_NO VARCHAR2(6) Foreign Key references s_order_no ofsales_order tableCHALLAN_DATE DATE Not NullBILLED_YN CHAR(1) values(‘Y’,’N’),Default ‘N’.

Sql Queries

Q.1 Find the name of all clients having ‘a’ as the second letter in their names.

Ans. Select name from Client_master where name like ’_a%’;

Q.2 Find out the clients who stay in a city whose second letter is ‘a’?

Ans. Select name from Client_master where city like ’_a%’;

Page 41: JAVA AND MYSQL QUERIES

41

Q.3 Find out the list of the client who stay in city ‘Bombay’ or city ‘Delhi’ or city ‘Madras’?

Ans. Select name from Client_master where city in (‘Bombay’ ,’Delhi’ ,’Madras’);

Q.4 List all the clients who are located in Bombay?

Ans. Select name from Client_master where city like ‘Bombay’;

Q.5 Print the list of clients whose bal_due are greater than value 10000?

Ans. Select * from Client_master where bal_due>10000;

Q.6 Find out the name of all the clients?

Ans. Select name from Client_master;

Q.7 Print the entire Client_master table?

Ans. Select * from Client_master;

Q.8 Retrieve the list of names and cities of all the clients?

Ans. Select name,city from Client_master;

Q.9 Count the total number of orders?

Ans. select count(reorder_lvl) from sales_order;

Q.10 Calculate the average price of all the product?

Page 42: JAVA AND MYSQL QUERIES

42

Ans. select avg(cost_price) from product_master;

Q.11 Calculate the minimum price of all the product?

Ans. select min (sell_price) from product_master;

Q.12 Determine the maximum and minimum product prices. Rename the title as max_price and min_price respectively?

Ans. Select max(sell_price) as max_price, min(sell_price) as min_price from product_master;

Q.13 Count the number of products having price greater than or equal to 1500?

Ans. select count(*) from product_master where cost_price>=1500;

Q.14 Find all the products whose qty_on_hand is less than reorder level?

Ans.select description from product_master where qty_on_hand< reorder_level;

Q.15 Print the description for each product?

Ans. . Select description from product_master group by description ;

Q.16 Find the value of each product sold?

Page 43: JAVA AND MYSQL QUERIES

43

Ans.select description, sum(s.qty_disp+product_rate) ”total sold value” from product_master p, sales_order_details s where p.product_no,s.product_no group by p.description;

Q.17 Calculate the average qty sold for each client that has a maximum order value of 15000.00?

Ans. select c.clent_no, c.client_name, avg(o.qty_disp) ”avg product” from client_master c, sales_order s, sales_order_details o where s.s_order_no = o.s_order_no and c.client_no=s.client_no group by c.client_no,c.client_name having max(o.qty_ordered*o.product_rate)>15000;

Q.18 Find out the total sales amount receiveble for the month of jan. it will be the sum total of all the billed orders for the month?

Ans.select o.s_order_no, sum(o.qty_ordered*o.product_rate) ”sum product” from sales_ order s, sales_order_details o where s.s_order_no=o.s_order_no and s.billed_yn like “y” and s.s_order_date like ‘%-jan-%’ group by o.s_order_no;

Q.19 Print the information of product_master, sales_order_detail table in the following format for all the records :- description total sales for the product

Page 44: JAVA AND MYSQL QUERIES

44

Ans. select p.description,sum(s.qty_ordered*s.product_rate) from product_master p,sales_order_details s where p.product_no=s.product_no;

Q.20 Change the delivery date of order number ‘010008’ to 16-08-96?

Ans.update sales_order set dely_date=’1996-08-16’ where s_order_no=’O10008’ ;

Q.21 Change the bal_due of client_no ‘c00001’ to 1000?

Ans. update Client_master set bal_due=1000 where client_no =’C0001’ ;

Q.22 Change the cost price of ‘1.22 Floppy Drive’ to Rs. 950.00?

Ans. update product_master set cost_price=’950.00’ where description=’1.22 Floppies’;

Q.23 Find out the maximum product rate for each product?

Ans.select description,max(cost_price) from product_master Group by product_no;

Q.24 Display the order number and day on which client placed their order?

Page 45: JAVA AND MYSQL QUERIES

45

Ans.select s_order_no, dayname(s_order_date) from sales_order group by client_no;

Q.25 Display the month(in alphabets) and date when the order must be delivered?

Ans. select monthname(dely_date),dely_date from sales_order;

Page 46: JAVA AND MYSQL QUERIES

46