java - 条码扫描仪逐个字符
问题描述
我已经尝试了一段时间,但无法找到一种仅在条形码扫描仪完成扫描后才能将文本从文本框中取出的方法。我正在使用 Swing 框架和 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;
}
}
解决方案
推荐阅读
- python - 我不明白何时使用 Pandas 系列以及何时使用 Pandas 单列数据框
- assembly - nasm x86 一种计算 e^x 的方法
- linux - 由于 Pandoc 不可用,无法在 Amazon Linux 2 上运行 rmarkdown
- java - 我的倒计时只减少了 1 并且没有更多,这是为什么呢?
- python-3.x - 使用 np.where 或 loc 更新 pandas 数据框的多列
- bash - 如何在 bash 中编写“rostopic echo”?
- java - 如果输出为0,如何打印数组?
- python - 将 Amazon Polly 的声音下载为 mp3 文件的网站
- python - 在 Pandas 中删除行时如何忽略 \N 转义?
- php - PHP + Apache:获取应用程序基本 URL 路径的通用方法