Jdbc odbc

Submitted by Devendra Ghag on
Sir, i want to get the selected value from JCombobox to ms access database...means the value which i selected it may me text or number simply i just want to print in ms access or stored on ms acess databse in selected comlumn???
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.sql.*;
  4. import javax.swing.*;
  5. public class GetValueOfJcomboBox implements ActionListener{
  6.  
  7.     String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
  8.     String petName ="";
  9.  
  10. private void createUI()
  11.     {
  12.         JFrame frame = new JFrame("JDBC All in One");
  13.         JComboBox petList = new JComboBox(petStrings);
  14.         petList.setPreferredSize(new Dimension(10,20));
  15.         petList.setSelectedIndex(0);
  16.         petList.addActionListener(this);
  17.         Container cn = frame.getContentPane();
  18.         JPanel p= new JPanel();            
  19.         p.add(petList);
  20.         cn.add(p);
  21.         Insets insets = cn.getInsets();
  22.         Dimension size = p.getPreferredSize();
  23.         p.setBounds(80 + insets.left, 135 + insets.top,
  24.                 size.width, size.height);
  25.         cn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS));
  26.         frame.add(petList);
  27.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         frame.pack();
  29.         frame.setVisible(true);
  30.     }
  31.  
  32. @Override
  33. public void actionPerformed(ActionEvent e)
  34. {
  35.     JComboBox cb = (JComboBox)e.getSource();
  36.     petName = (String)cb.getSelectedItem();
  37.     for(String s : petStrings)
  38.     {
  39.         if(petName.equals(s))
  40.         {
  41.             addOperation();
  42.             System.out.println("Value added successfully");
  43.         }      
  44.       }
  45.     }
  46.  
  47.  private void addOperation()
  48.  {
  49.     try
  50.     {
  51.         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  52.         Connection con = DriverManager.getConnection("jdbc:odbc:swing");
  53.         String sql = "INSERT INTO PetTable(petName) Values('"+petName+"')";
  54.         Statement st = con.createStatement();
  55.         st.execute(sql);
  56.         JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added",
  57.                 JOptionPane.INFORMATION_MESSAGE);
  58.     }
  59.     catch(Exception e)
  60.     {
  61.         JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
  62.                 JOptionPane.ERROR_MESSAGE);
  63.     }
  64.   }
  65.  public static void main(String args[])
  66.  {
  67.      GetValueOfJcomboBox gvb = new GetValueOfJcomboBox();
  68.      gvb.createUI();
  69.  }
  70. }// class closed