How to passed the ID's in MAP how to stored whaterver select the name that only ID should store in db using SWING/AWT?

Ads
 

How to passed the ID's in MAP how to stored whaterver select the name that only ID should store in db using SWING/AWT?

Hi,

I am working on SWING/AWT application,I write select box programs in swings.

my proble is what ever the name is selected that id should be stored in database how to passed the ID's in MAP how to stored whaterver select the name that ID should store in database

public class ComboBox{ 

public static void main(String[] args) { 
ComboBox b = new ComboBox(); 
} 

public ComboBox(){ 
String byers [] = {"radha","rani","vani","devi"}; 
JFrame frame = new JFrame("Creating a JComboBox Component"); 
JPanel panel = new JPanel(); 
combo = new JComboBox(byers); 
........ 
combo.addItemListener(new ItemListener(){ 
public void itemStateChanged(ItemEvent ie){ 
String str = (String)combo.getSelectedItem(); 
txt.setText(str); 
} 
}); 

} 
}

plz give an example

View Answers

June 23, 2012 at 6:12 PM

Here is a required code.

import java.sql.*;
import java.awt.*;
import java.util.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class ComboBox{

    public static void main(String[] args) throws Exception{
        JFrame f=new JFrame();
        f.setLayout(null);
        JLabel lab=new JLabel("Select Name:");

        String byers [] = {"radha","rani","vani","devi"};
        final JComboBox combo=new JComboBox(byers);
        final Map map=new HashMap();
        for(int i=0;i<byers.length;i++){
        map.put(new Integer(i+1),byers[i]);
        }

        ActionListener actionListener = new ActionListener(){
        public void actionPerformed(ActionEvent actionEvent) {
        ItemSelectable is = (ItemSelectable)actionEvent.getSource();
        String name=selectedString(is);
        System.out.println(name);
        Set pset=map.entrySet();
         int key=0;
         Iterator piterator=pset.iterator();
         while(piterator.hasNext())
            {
               Map.Entry m =(Map.Entry)piterator.next();
               String value=(String)m.getValue();
            if(value.equals(name)){
                    key=(Integer)m.getKey();
            }
           }
        try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" );
        Statement st=conn.createStatement();
        st.executeUpdate("insert into data1(id) values("+key+")");
        System.out.println("Inserted");
        }
        catch(Exception e){
        System.out.println(e);
        }
        }
       };
       combo.addActionListener(actionListener);
       lab.setBounds(20,20,100,20);
       combo.setBounds(120,20,80,20);
       f.add(lab);
       f.add(combo);
       f.setVisible(true);
       f.setSize(300,120);
    }
    static private String selectedString(ItemSelectable is) {
    Object selected[] = is.getSelectedObjects();
    return ((selected.length == 0) ? "null" : (String)selected[0]);
  } 
}

Ads









Related Tutorials/Questions & Answers:
How to passed the ID's in MAP how to stored whaterver select the name that only ID should store in db using SWING/AWT?
How to write a select box and id should be stored in database?
Advertisements
How to store data entered in JSP page by a user in XML file & later retrieval of data using id at other page
How to get Button Id ?
How to select only .txt file to be zipped using java?
How to print UIButton id
How I Upload File and Store that file name in Database using JSP
How i can send testing mail on my id using java?
how to retrieve all id in elasticsearch
how to retrieve all id in elasticsearch
store the employee ID,name,login time and logout time of the employee in database
how to validate national ID number
how to store image in folder and stored image path in mysql database using JSP
janusgraph get vertex id - how to get id of vertex
how to send smtp mail to entered user mail id after user registered using javascript?
how to send smtp mail to entered user mail id after user registered using javascript?
How to identify the radio button id and row id in a table in jsp?
How to code a Product id search engine?
How to store user name,city,state,image path into database and image into folder using jsp
[JSP] How to pass back ID to previous page.
How to Store Image using JSF
how to operate on select box using ajax in struts2?
HOW TO DISPLAY ID IN TEXTBOX BASED ON COMBOBOX SELECTION IN A SAME PAGE
HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP?
id
read from file and store using hash map
how to get data from list to map with out using a loop?
Stoting file name in Map
ModuleNotFoundError: No module named 'db-obcuscate-id'
delete row using id
Printing Session Id Using Variable

Ads