
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???

Hi Devendra, Here I am giving a sample code against your problem. you can read this complete tutorial from the link given below
http://www.roseindia.net/java/example/java/swing/store-jcombobox-selecteditem-value-msaccess.shtml
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
public class GetValueOfJcomboBox implements ActionListener{
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
String petName ="";
private void createUI()
{
JFrame frame = new JFrame("JDBC All in One");
JComboBox petList = new JComboBox(petStrings);
petList.setPreferredSize(new Dimension(10,20));
petList.setSelectedIndex(0);
petList.addActionListener(this);
Container cn = frame.getContentPane();
JPanel p= new JPanel();
p.add(petList);
cn.add(p);
Insets insets = cn.getInsets();
Dimension size = p.getPreferredSize();
p.setBounds(80 + insets.left, 135 + insets.top,
size.width, size.height);
cn.setLayout(new BoxLayout(cn,BoxLayout.Y_AXIS));
frame.add(petList);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
petName = (String)cb.getSelectedItem();
for(String s : petStrings)
{
if(petName.equals(s))
{
addOperation();
System.out.println("Value added successfully");
}
}
}
private void addOperation()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:swing");
String sql = "INSERT INTO PetTable(petName) Values('"+petName+"')";
Statement st = con.createStatement();
st.execute(sql);
JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added",
JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage(),"Error",
JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String args[])
{
GetValueOfJcomboBox gvb = new GetValueOfJcomboBox();
gvb.createUI();
}
}// class closed
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.