如何检查数据库中是否已存在该值? [英] How do I check if the value already exists in the database?

查看:94
本文介绍了如何检查数据库中是否已存在该值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中创建一个客户端 - 服务器应用程序。在服务器上我有一个Microsoft Access数据库(SQL),在客户端我有JFrame形式。我有一个客户端和服务器之间的通信,它的工作没有问题。我的表单有一个特定的文本字段(我们称之为txtField)。我需要通过表单插入某些数据到我的数据库。但在此之前,需要检查在txtField中输入的值是否已经存在于数据库中。我如何做到这一点,以及在哪里写这个代码,我猜在形式?
感谢

I am creating a client-server app in Java. On server I have a Microsoft Access database (SQL), and on client I have JFrame form. I have a communication between client and server and it works no problem. My form has a certain text field (let's call it txtField).I need to insert certain data into my database through the form. But before that, it needs to be checked whether the value that's been typed in the txtField already exists in the database. How do I do this, and where to write this code, I guess in the form? Thanks

推荐答案


我需要通过表单将特定数据插入数据库。但在此之前,需要检查在txtField中输入的值是否已经存在于数据库中。

I need to insert certain data into my database through the form. But before that, it needs to be checked whether the value that's been typed in the txtField already exists in the database.

否。如果尚未在数据库中,则需要将其插入数据库。正确的方法是尝试插入并捕获失败。否则,您将在检查和插入之间创建一个计时窗口问题,在此期间另一个客户端可以执行插入。

No. You need to insert it into the database if it isn't already there. The correct way to do that is to try the insert and catch the failure. Otherwise you are creating a timing-window problem between your check and your insert, during which another client could do the insert.

这篇关于如何检查数据库中是否已存在该值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆