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 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]);
  } 
}









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 select only .txt file to be zipped using java?
How I Upload File and Store that file name in Database using JSP
store the employee ID,name,login time and logout time of the employee in database
how to store image in folder and stored image path in mysql database using JSP
How to get Button Id ?
How i can send testing mail on my id using java?
how make ID - Ajax
How to print UIButton id
How to store user name,city,state,image path into database and image into folder using jsp
how to retrieve all id in elasticsearch
how to retrieve all id in elasticsearch
read from file and store using hash map
Stoting file name in Map
How to Store Image using JSF
how to operate on select box using ajax in struts2?
how to validate national ID number
ModuleNotFoundError: No module named 'db-obcuscate-id'
how to generate a bar chart on a JSP PAGE using the arraylist object passed form the servlet.(using jfreechart)
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?
janusgraph get vertex id - how to get id of vertex
How to get an Id vaue - JSP-Servlet
Inserting id in place of name to database
HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP?
How to identify the radio button id and row id in a table in jsp?
How to get data from DB in to Text box by using Jsp & Ajax
delete row using id
id
How to code a Product id search engine?
how to get data from list to map with out using a loop?
How To Store Image Into MySQL Using Java
Printing Session Id Using Variable
How to browse excel file and stored the contents into the database using jsp/servlet?
[JSP] How to pass back ID to previous page.
upload image and fields.....fields is id name.....
how to operate on select box using ajax in struts2?
How to get an Id vaue - JSP-Servlet
How to give value for select in HTML dynamically using javascript
how to iterate a map in java using for each
how to iterate a map in java using for each
how to iterate a map in java using for each
upload image and fields.....fields is id name.....
how I do select from select in hql
HOW TO DISPLAY ID IN TEXTBOX BASED ON COMBOBOX SELECTION IN A SAME PAGE
how to save html form data into .csv file using only jsp.
How to make session control using only JSP programming language
ID using get the databse latest value and displayed in jsp

Ads