Practical 13
Aim:- Develop a program to present a set of choice for user to select a product and display the price
of product.
import java.awt.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.*;
public class pract13 extends Frame implements WindowListener,ActionListener
static final String url="jdbc:mysql://localhost/gtu";
static final String usr="root";
static final String psd="Vrajesh";
static Connection conn;
static Statement stmt;
TextField uname; JTextArea dis;
Checkbox item1,item2,item3,item4;
Button done;
static Integer id[];
static String item[];
static Float prize[];
static Float total;
public pract13()
addWindowListener(this);
setSize(500,500);
setTitle("Product List");
setVisible(true);
setLayout(null);
Label ename=new Label("Enter Name:");
ename.setBounds(10,40,70,20);
add(ename);
uname=new TextField(20); uname.setBounds(100,40,100,20);
add(uname);
Label sel=new Label("Select Products:");
sel.setBounds(10,70,100,20);
add(sel);
item1=new Checkbox(item[0]);
item1.setBounds(110,70,50,20);
item2=new Checkbox(item[1]);
item2.setBounds(110,90,50,20);
item3=new Checkbox(item[2]);
item3.setBounds(110,110,50,20);
item4=new Checkbox(item[3]);
item4.setBounds(110,130,50,20);
add(item1);
add(item2);
add(item3);
add(item4);
done=new Button("Purchase");
done.setBounds(30,170,70,20);
done.addActionListener(this); add(done);
dis=new JTextArea(10,50);
dis.setOpaque(false);
dis.setBounds(50,200,200,60);
add(dis);
public void windowOpened(WindowEvent we){
}
public void windowClosing(WindowEvent we){
setVisible(false);
System.exit(0);
public void windowClosed(WindowEvent we){}
public void windowIconified(WindowEvent we){}
public void windowDeiconified(WindowEvent we){}
public void windowActivated(WindowEvent we){}
public void windowDeactivated(WindowEvent we){}
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand()=="Purchase"){if(item1.getState()){
total=prize[0];
if(item2.getState()){
total=total+prize[1];
if(item3.getState()){
total=total+prize[2];
if(item4.getState()){
total=total+prize[3];
dis.setText("Hello "+uname.getText()+"\n Your Total Amount is:"+total
+"\nHappy Shopping :)");
public static void con(){
try{Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection(url,usr,psd);
stmt=conn.createStatement();
System.out.println("Connected..");
ResultSet rs=stmt.executeQuery("Select * from product");
id=new Integer[4];
item=new String[4];
prize=new Float[4];
int i=0;
while(rs.next()){
id[i]=rs.getInt("p_id");
item[i]=rs.getString("product");
prize[i]=rs.getFloat("price");
i++;
catch(Exception e){
e.printStackTrace();
}public static void main(String[] args) {
con();
new pract13();
}
Output:-