Home Answers Viewqa Swing-AWT How to write a select box and id should be stored in database?

 
 


indira
How to write a select box and id should be stored in database?
2 Answer(s)      11 months ago
Posted in : Swing AWT

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 Pages:
id

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.