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

Ads
 

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

Ads









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
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
how I do select from select in hql
how to select second combobox value .
how to write this program
how to create a combo box in html
How to get Button Id ?
how to insert check box
How to print UIButton id
How to write Java Program
How to write in File in Java
How to get the values from the Combo Box - JSP-Servlet
How to retrieve data by using combo box value in jsp? - JSP-Servlet
databases
databases
databases
databases
databases

Ads