2013-04-24 74 views
-1

我试图插入一个值到一个JDBC,我从一个组合框中获取一个值,我必须将其转换为一个int,但查询不是认识到它是一个int?它打印出来作为一个数字控制台。是一个普通的int int

下面是最能复制问题的代码示例。

我已经尝试将输入转换为字符串,然后解析它,但它仍然不会识别它。它就像它不会识别整数。 我有点难住。 感谢

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(); 
    } 

} 

错误: 异常在线程 “AWT-EventQueue的 - 0” java.lang.Error的:未解决的问题,编译: qaun不能被解析为一个变量

+1

您的代码和问题似乎没有与JDBC有关。你能简化代码和问题到你有问题的部分吗? – 2013-04-24 20:11:25

回答

0

It prints out to console as a number

此代码是相同

int quan = (int)qty.getSelectedItem();  
System.out.println(quan); 

相同

System.out.println(Integer.toString(quan)); 

无法将数字打印为int,因为控制台只能显示字符,因此必须将其转换为文本。

+0

他的问题是编译错误与变量名称 – Kal 2013-04-24 20:12:45

3

你的变量是quan wheras您使用qaun

看那con.insertProduct(qaun);

编译错误显示你这分明

错误:异常在线程 “AWT-EventQueue的 - 0” 的Java。 lang.Error:未解决的编译问题:qaun无法解析为变量

+0

作为问题的意义。 – 2013-04-24 20:10:17

+0

错过了我原来的回应..可能是OP做了什么:-) – Kal 2013-04-24 20:11:34

+0

编译器是否检测到这个错误并阻止它编译。除非您生成了字节码,否则不应在运行时检测到它。 – 2013-04-24 20:13:30