
How to fetch values from database based on dropdown list selection?

public class Swapping{
static void swap(int i,int j){
int temp=i;
i=j;
j=temp;
System.out.println("After swapping i = " + i + " j = " + j);
}
public static void main(String[] args){
int i=1;
int j=2;
System.out.prinln("Before swapping i="+i+" j="+j);
swap(i,j);
}
}
In the above code, the values of i and j are function arguments.

import java.sql.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class JComboBoxJTable{
public static void main(String[] args) throws Exception{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab=new JLabel("Select");
final JComboBox combo=new JComboBox();
combo.addItem("--Select--");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
final Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from employee");
while(rs.next()){
combo.addItem(rs.getString("name"));
}
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
ItemSelectable is = (ItemSelectable)actionEvent.getSource();
String nn=selectedString(is);
System.out.println("Selected: " + nn);
}
};
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]);
}
}
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.