I'm trying to insert a value into a JDBC, I'm getting a value from a combo box in which I have to cast to an int but the query isn't recognizing it as an int? It prints out to console as a number.
Here's a sample of code that best replicates the problem.
I've tried turning the input to a string and then parsing it, but it still won't recognize it. Its like it won't recognize the int. I'm a bit stumped. Thanks
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AddingItemToComboBox implements ActionListener{
JButton click = new JButton("Click me");
JComboBox qty = new JComboBox();
public AddingItemToComboBox(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setSize(500,500);
click.addActionListener(this);
qty.setBounds(10,270, 150, 20 );
qty.setSize(80,30);
qty.addItem(1);
qty.addItem(2);
qty.addItem(3);
panel1.add(qty);
panel1.add(click);
frame.add(panel1);
frame.setSize(300, 200);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == click){
int quan = (int)qty.getSelectedItem();
System.out.println(quan);
//Connection to database
// Here is the problem "quan"
con.insertProduct(qaun);
}
}
public static void main(String[] args){
AddingItemToComboBox aic = new AddingItemToComboBox();
}
}
Error: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: qaun cannot be resolved to a variable
Your variable is quan
wheras you are using qaun
Look at the con.insertProduct(qaun);
The compile error shows you this clearly
Error: Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: qaun cannot be resolved to a variable
It prints out to console as a number
This code is the same as
int quan = (int)qty.getSelectedItem();
System.out.println(quan);
is the same as
System.out.println(Integer.toString(quan));
There is no way to print a number as an int as your console can only display characters so it must turn it into text.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.