Select info from MS SQL Server to display in JComboBox
843785Jul 27 2008 — edited Jul 27 2008Hi all,
I have created a Java Application using JCreator and had encountered a problem which i could not solve for weeks. As i am new to Java, i'm not sure what's wrong with my code. Basically, i copied a code sample from another project (using microsoft access) and edited it accordingly. I tried part of the code in another file and manage to retrieve information from MS SQL Server database. But i do know how to put the values into the JComboBox. I hope someone can help me with this.
Below is the full code that i have tried. please help me out. i'm running out of time.
Thanks in advance
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.util.*;
public class InputComboBox extends JFrame implements ActionListener {
private RFIDLogisticsDB inputSelected;
private static JComboBox itemBox;
public InputComboBox(String Item_ID) {
inputSelected = new RFIDLogisticsDB();
add(new JLabel(Item_ID));
add(itemBox);
}
public RFIDLogisticsDB getItem() {
return inputSelected;
}
public String getR(){
return (String)itemBox.getSelectedItem();
}
public void actionPerformed(ActionEvent e){
Object source=e.getSource();
}
public static void main(String args[]){
String Item_ID;
String Item_Status;
String query = "SELECT * FROM Item WHERE Item_Status = 'warehouse'";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:RFID Logistics");
PreparedStatement pstmt = con.prepareStatement(query);
ResultSet rs= pstmt.executeQuery();
while (rs.next())
{
Item_ID = rs.getString("Item_ID");
itemBox.addItem(Item_ID);
}
}
catch(ClassNotFoundException f)
{
f.printStackTrace();
}
catch(SQLException f)
{
f.printStackTrace();
}
JFrame frm=new JFrame();
InputComboBox p=new InputComboBox("Item to be shifted");
frm.getContentPane().add(p);
frm.pack();
frm.setVisible(true);
}
}