Wednesday 22 February 2017

Develop a programme to present a set of choice for user to select a product and display the price of product.

*How to execute Programme.

    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)FileName
     
GUIList.java : This file include all GUI of Frame and One               Listener Which is call other class for get Data from Database.

DataDB.java:  In this file First i make 2 static int variable which       is use to get id or price globally.Then i have make one getData         function with one parameter.getData(String Name) Function             Create  Connection & Pass Query & get Data From Database.                                       
 DriverClass.java : This Class file define all basic parameter of          Database Connectivity.


Code:

GUIList.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Prac13;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author Queue
 */
public class GUIList  extends JFrame implements ItemListener{ 
    
    JComboBox combo;
    
    JPanel p,p2;
    JLabel Id,IdValue,Dep,DepValue;
    
    public String names[]={" ","Laptop","Desktop","KeyBord","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();
    }
    @Override
    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);
        }
        
    }

}



DataDB.Java



package Prac13;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Queue
 */
public class DataDB {
    
    public static int id=0;
    public static int p_price=0;
    
    DriverClass d=new DriverClass();
    
    public void getData(String Name) throws ClassNotFoundException, SQLException
    {
        Class.forName(d.Driver);
        Connection con=DriverManager.getConnection(d.Path,d.UserName,d.password);
        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");
       }
    }

}



DriverClass.java

package Prac13;

import p12.*;
public class DriverClass {
    
    public String Driver="com.mysql.jdbc.Driver";
    public String UserName="root";
    public String password="ujash7878";
    public String Path="jdbc:mysql://localhost:3306/student";
    

}


Download Source Code

Output:








Thanks ;)
Happy Coding
   


2 comments:

  1. Join Now Java course , Online Java course
    https://corosocial.com/read-blog/141413_future-of-java-technology-will-help-you-get-there.html
    APTRON provides the best Java course , Online Java course for beginners as well as advanced programmers. Java course, Online Java course will help you to learn and nourish your programming skills in Java.

    ReplyDelete