条形码扫描仪字符的字符

时间:2018-05-18 16:39:07

标签: java swing documentlistener

我已经尝试了一段时间,只有在条形码扫描仪完成扫描后才能找到一种方法从文本框中取出文本。我正在使用Swing Framework和Java。如果将文本粘贴(Ctrl + V)到JTextbox中,我的代码可以工作,但条形码扫描器不起作用,因为我的方法依次为条形码的每个字符运行。

这是我的代码:

public class pos extends javax.swing.JFrame{
private Timer updateTimer;
private ResultSet data;
ResultSet rs = null;
PreparedStatement pst = null;

public pos() {
    initComponents();
    this.setLocationRelativeTo(null);
    Function f = new Function();

            updateTimer = new Timer(10, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) { 
                ResultSet rs = null;
                rs = f.find(prodID.getText());
                data = rs;
                }
            });
            updateTimer.setRepeats(false);

    prodID.getDocument().addDocumentListener(new DocumentListener(){

        @Override
        public void insertUpdate(DocumentEvent arg0){
            warn();
        }
        @Override
        public void removeUpdate(DocumentEvent arg0){ 
            warn();
        }
        @Override
        public void changedUpdate(DocumentEvent arg0){
            warn();
        }          

    });


}

protected void warn(){
            try{
                if(this.data.next()){
                    prodName.setText(this.data.getString("prodName"));
                    priceField.setText(String.valueOf(new Double(this.data.getString("price"))));
                }else{
                    JOptionPane.showMessageDialog(null, "No Data for this ID");
                }
            }catch(Exception ex)
            {
                JOptionPane.showMessageDialog(null, ex);
            }
            updateTimer.restart();
        }

public Connection getConnection(){
    Connection con = null;
    try{
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/fmgaccount","root","");
    }catch(Exception ex){

    }
    return con;
}



public class Function{
    Connection con = null;
    public ResultSet find(String s){
        try{
        con = getConnection();
        pst = con.prepareStatement("SELECT prodName, price FROM deliverystocks WHERE prodID=?");
        pst.setString(1,s);
        rs = pst.executeQuery();
        }catch(Exception ex)
                {
                   JOptionPane.showMessageDialog(null, ex);
                }
         return rs;
    }

}

0 个答案:

没有答案