JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?

huangapple go评论88阅读模式
英文:

JFrame checkbox data stored in a variable. How can I input this into a mysql query?

问题

我有2个复选框选项,允许用户在预订表单中选择住几个晚上。

[JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?]

在添加了所有客户详细信息,如姓名、地址、电子邮件等之后,我正在将这些信息输入到 MySQL Workbench 数据库进行存储。

因为我有两个复选框选项用于选择晚上的部分,根据用户的选择,这些选项将存储在一个变量中,我该如何将其输入到我的数据库中呢?由于复选框被称为两个不同的名称,我无法将它们命名为相同的内容。我该如何在代码中稍后使用这个变量来查找用户的选择。

从变量字符串中获取文本以用于 SQL 查询。

[JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?]

如果这些内容没有意义,对此我感到抱歉,我刚刚开始接触这个领域,如果需要更多的解释,请随时提问。

我目前的 SQL 查询如下所示:

[JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?]

从这个图片中可以看出,其他变量可以被输入,但我无法弄清楚如何使 nightsChosen 变量正常工作。

英文:

I have 2 checkbox options to allow a user to select how many nights they want to stay on the booking form.

[JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?strong text

Once all of the customer details have been added, such as name, address, email..., I am inputting these into a mysql workbench database to be stored.

Because I have two checkbox options for the nights selected section, which will be stored in one variable depending on what the user selects, how am I able to input this into my database? The checkboxes are called 2 different names as I am unable to call them the same thing. How can I use the variable later on in the code to find what the user selects.

Get text from variable strings for the sql query

JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?

Sorry if this doesnt make any sense, I am new to this so please ask if you need any more clarification JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?

my sql query so far

JFrame复选框数据存储在变量中。如何将其输入到mysql查询中?

As you can see from this image, the other variables are able to be input, but I cant figure out how to get the nightsChosen one to work.

答案1

得分: 0

有几件事情你需要处理:

  • 要获取复选框的值,使用复选框实例上的 isSelected() 方法。如果复选框被选中,它将返回 true。由于它们是不同的实例,你知道一个是用于7天,一个是用于14天,因此根据选中哪个,你就知道要使用的值。

  • 从用户体验的角度来看,如果这些选择是互斥的(你只能选择7天或14天,不能同时选择两者),你应该使用单选按钮,而不是复选框。

  • 在你的 SQL 查询中,你正在使用预处理语句(这是很好的)。但你仍然在手动构建字符串来形成 SQL。这会使你容易受到 SQL 注入攻击。你应该这样做:在 SQL 字符串中使用占位符,并将变量绑定到占位符上。例如:

    PreparedStatement stmt = conn.prepareStatement("select * from mytable where id = ?");
    stmt.setInt(1, yourVariable);

  • 未来请不要发布显示代码的图片,而是请将代码文本直接包含在问题本身中,以便在 StackOverflow 上发布。这样可以使问题更易于搜索,也可以使社区更轻松地查看/尝试你的代码。

英文:

There are a few things you should address:

  • to get the value of the check box, use the isSelected() method on the check box instance. It'll return true if the box is checked. Since they're different instances, you know which one is for 7 days and which is for 14 so depending on which one is checked, you'll know the value to use.

  • from a UX perspective, if these choices are mutually exclusive (you can only pick 7 or 14 days, never both), you should use radio buttons rather than checkboxes.

  • in your SQL query, you're using a prepared statement (which is good). But you're still forming the sql by manually forming the string. This would leave you open to a SQL injection attack. The way you should do it is by using placeholder values in the sql string and then binding the variables to the placeholders. For instance:

    PreparedStatement stmt = conn.prepareStatement("select * from mytable where id = ?");
    stmt.setInt(1, yourVariable);

  • Rather than post images showing your code, in the future, please include the text in the question itself when posting to StackOverflow. It makes the questions more searchable and it enables the community to more easily see/try your code.

huangapple
  • 本文由 发表于 2020年10月25日 20:10:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64523519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定