Practical-13 Develop a program to present a set of choice for user to select a product and display the price of product.

Give Project Name as practical13

1. First Make a DataBase Name is Student.

2. Make a Table Name is product. 

    Create table product(id int(10),
    p_name varchar(30),
    p_price int(10));

3. Insert Data.

     insert into product values(01,'Laptop',35000);
     insert into product values(02,'Desktop',30000);
     insert into product values(03,'Keyboard',700);
     insert into product values(04,'Mouse',500);

4. File Name
     
GUIList.java : This file include all GUI of Frame.
DataDB.java:  Create  Connection & Pass Query & get Data From Database.
DriverClass.java : This Class file define all basic parameter of Database Connectivity.

Code:

GUIList.java:

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class GUIList extends JFrame implements ItemListener {
    JComboBox combo;
    JPanel p,p2;
    JLabel Id,IdValue,Dep,DepValue;
    
    public String names[]={" ","Laptop","Desktop","Keyboard","Mouse"};
    DataDB db=new DataDB();
    GUIList()
    {
        p=new JPanel();
        combo=new JComboBox(names);
        p.add(combo);
       
        p2=new JPanel();
        Id =new JLabel("ID:");
        IdValue=new JLabel("");
        Dep=new JLabel("Department:");
        DepValue=new JLabel("");
        
        p2.add(Id);
        p2.add(IdValue);
        p2.add(Dep);
        p2.add(DepValue);
        
        combo.addItemListener(this);
        add(p);  
        add(p2);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500,500);
        setVisible(true);
    }
    public static void main(String[] args)
    {
        GUIList gui=new GUIList();
    }
    public void itemStateChanged(ItemEvent e)
    {
        String Name=(String) combo.getItemAt(combo.getSelectedIndex());
        try
        {
            db.getData(Name);
            IdValue.setText(String.valueOf(DataDB.id));
            DepValue.setText(String.valueOf(DataDB.p_price));
        }
        catch(ClassNotFoundException ex)
        {
            Logger.getLogger(GUIList.class.getName()).log(Level.SEVERE, null,ex);
        }
        catch(SQLException ex)
        {
            Logger.getLogger(GUIList.class.getName()).log(Level.SEVERE, null,ex);
        }
    }
}

DriverClass.java:

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DriverClass {
    public String Driver="com.mysql.cj.jdbc.Driver";
    public String UserName="root";
    public String password="root";
    public String Path="jdbc:mysql://localhost:3306/student";
}

DataDB.java:

import java.sql.*;

public class DataDB {
    public static int id=0;
    public static int p_price=0;
    
    public void getData(String Name) throws ClassNotFoundException, SQLException
    {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
        System.out.println("Connection Establish");
        PreparedStatement ps=con.prepareStatement("select *from product where p_name=? ");
        ps.setString(1, Name);
        ResultSet rs=ps.executeQuery();
        
        while(rs.next())
        {
            id=rs.getInt("id");
            p_price=rs.getInt("p_price");
       }
    }
}

Output:


Comments

Popular posts from this blog

Write a program to check whether empty query string is entered in Asp.Net.

Mandavrayji Temple, Muli, Surendranagar, Gujarat

Write a program to change color of Label text control programmatically in Asp.Net.