How to write a select box and id should be stored in database?

How to write a select box and id should be stored in database?

Hi,

How to write a select box and select the name(devi) regarding name id(like 60) should be stored in database using SWINGS concept

plz help

View Answers

June 20, 2012 at 12:52 PM

You haven't clarified your query properly. Anyways, here is an example of inserting the id value into the database according to the name selected from the drop down.

import java.sql.*;
import java.awt.*;
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:");
        final JComboBox combo=new JComboBox();
        combo.addItem("George");
        combo.addItem("Maria");
        combo.addItem("John");
        combo.addItem("Jennifer");
        combo.addItem("Kate");

        ActionListener actionListener = new ActionListener(){
        public void actionPerformed(ActionEvent actionEvent) {
        ItemSelectable is = (ItemSelectable)actionEvent.getSource();
        String name=selectedString(is);
        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("update data1 set id=60 where name='"+name+"'");
        System.out.println("Inserted");
        }
        catch(Exception E){}
        }
       };
       combo.addActionListener(actionListener);
       combo.setEditable(true);
       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]);
  } 
}

June 20, 2012 at 1:14 PM

Here is another example that simply insert the selected combobox value and id into the database.

import java.sql.*;
import java.awt.*;
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:");
        final JComboBox combo=new JComboBox();
        combo.addItem("George");
        combo.addItem("Maria");
        combo.addItem("John");
        combo.addItem("Jennifer");
        combo.addItem("Kate");

        ActionListener actionListener = new ActionListener(){
        public void actionPerformed(ActionEvent actionEvent) {
        ItemSelectable is = (ItemSelectable)actionEvent.getSource();
        String name=selectedString(is);
        System.out.println(name);
        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,name) values(60,'"+name+"')");
        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 write a select box and id should be stored in database?
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?
Advertisements
I have a tex box. in that i want user should enter data in the format specified(for eg--a_b_c_d_e_)how to write code for it.
how to operate on select box using ajax in struts2?
how to operate on select box using ajax in struts2?
How to implement ajax in struts2 to operate on select box in jsp
how to display(update) current date and month in select box on selecting the year.
select box and text box validations
Select Box Validation in JavaScript
How to list databases in PostgreSQL
Dynamic select box - Ajax
Multiple select box
Select Box question
dynamic select box
dynamic select box
Write a C/C++ program to show the result of a stored procedure
display from select box - JSP-Servlet
how I do select from select in hql
radio button selection should move the control to a text box that needs to be complete
how make ID - Ajax
How to get Button Id ?
databases
databases
databases
databases
databases
how to select second combobox value .
how to write this program
How to print UIButton id
JSP - Checkbox remain checked although checked values should depend on values stored in a stateful session bean
write a program to create an vector and listeterator.and value should be enter through keyboard.
write a program to create an arraylist and listeterator.and value should be enter through keyboard.
write a program to create an vector and listeterator.and value should be enter through keyboard.
write a program to create an vector and listeterator.and value should be enter through keyboard.
write a program to create an arraylist and listeterator.and value should be enter through keyboard.
how to create a combo box in html
How to create an input box?
How to create a confirmation box?
How to retrieve data by using combo box value in jsp? - JSP-Servlet
How to write javascripts - JSP-Servlet
Use of Select Box to show the data from database
Databases
How to write javascripts - JSP-Servlet
how to insert check box
my table should be reseted to new value upon the selection of the combo box..
How to get the values from the Combo Box - JSP-Servlet
How to retrieve data by using combo box value in jsp? - JSP-Servlet
Passing Multi select list box values using ajax to get values to dependent list box
How to write Java Program
How to write in File in Java

Ads