首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将输入数据添加到SQL值?

如何将输入数据添加到SQL值?
EN

Stack Overflow用户
提问于 2018-12-27 07:42:54
回答 3查看 72关注 0票数 0

我们想要建立一个医疗信息系统。在这个制度下,我们必须买药。当用户购买药品时,他/她必须输入条形码和购买药品的号码。我们希望获取要服用的药物数量,并将此值添加到SQL表中。但我们有个错误。

代码语言:javascript
复制
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '10013''where Barcode='10013'' at line 1

SQL表

这是我的代码:BuyMedicine.java

..。

代码语言:javascript
复制
BarcodeField = new JTextField();
    BarcodeField.setBounds(130, 26, 294, 21);
    contentPane.add(BarcodeField);
    BarcodeField.setColumns(10);

    NumberField = new JTextField();
    NumberField.setBounds(130, 86, 294, 21);
    contentPane.add(NumberField);
    NumberField.setColumns(100);

    JButton btnBuy = new JButton("Buy");
    btnBuy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root","");
                Statement stmt = con.createStatement();
                String x = "Select Stock from medicinelist where Barcode='" + BarcodeField.getText()+"'";
                PreparedStatement preparedStmt2 = con.prepareStatement(x);
                preparedStmt2.execute();
                String sql = "Update medicinelist set Stock='" +Integer.parseInt(NumberField.getText())+x
                +"'where Barcode='"+ BarcodeField.getText()+"'";
                PreparedStatement preparedStmt = con.prepareStatement(sql);
                preparedStmt.executeUpdate();


                JOptionPane.showMessageDialog(null, "Done!!!");


                con.close();

            }catch(Exception e) { System.out.print(e);}
        }

});

..。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-27 09:36:47

在查询中

代码语言:javascript
复制
 '" +Integer.parseInt(NumberField.getText())+ x +"'where  

放置+“'x后面的查询如下

代码语言:javascript
复制
 '" +Integer.parseInt(NumberField.getText())+"'"+ x +" where

并为干净的查询留出空间。

票数 0
EN

Stack Overflow用户

发布于 2018-12-27 11:06:56

字符串x=“从医药列表中选择股票,其中Barcode=‘+BarcodeField.getText()+’‘;

String sql = "Update set Stock='“+Integer.parseInt(NumberField.getText())+x +”Barcode='"+ BarcodeField.getText()+"'";

所以string sql变成

String sql = "Update set Stock='“从Barcode=‘’+BarcodeField.getText()+‘’BarcodeField.getText+ Barcode='"+ BarcodeField.getText()+‘’”的医药列表中获得的股票;

错误也是如此:)

在结果集中收集"string x“查询输出,并在"string”中使用它。

票数 1
EN

Stack Overflow用户

发布于 2018-12-27 09:22:41

代码语言:javascript
复制
      +"'where Barcode='"+ BarcodeField.getText()+"'";

单引号和之间没有空格,其中。提供它,然后再运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53941482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档