检查MySQL表中是否存在ANYYY值

mqkwyuun  于 5个月前  发布在  Mysql
关注(0)|答案(1)|浏览(39)

我正在做一个关于用户帐户创建的Java程序,我想检查是否有任何用户名或密码之前在表中。

String kullanici_adi = KullaniciAdi.getText();
       String sifre = Sifre.getText();
       String query = "CREATE DATABASE IF NOT EXISTS BUTTERFLYFLY";
       Statement SQL=null;
       Statement SQLD=null;
       String yol="jdbc:mysql://localhost:3306/";
       String yol2="jdbc:mysql://localhost:3306/butterflyfly";
        try {
            Connection con= DriverManager.getConnection(yol, "root", "");
            SQL = con.createStatement();
            SQL.close();
            con.close();
            Connection cond=DriverManager.getConnection(yol2, "root", "");
            Main m=new Main();
            String x=m.oturum_kurulumu;
            SQLD= cond.createStatement();
            SQLD.executeUpdate(x);

            /* If there is a username or password in table pass this
            PreparedStatement stmt = cond.prepareStatement("INSERT INTO Oturum (Kullanici_Adi, Sifre)\n" +
"VALUES (?, ?);");
            stmt.setString(1, kullanici_adi);
            stmt.setString(2, sifre);
            stmt.executeUpdate();

*/
        } catch (SQLException e) {
            System.out.println("Bir hata olustu");
            System.out.println(e.getMessage());
        }

字符串
我想在表中检测值,我研究了一下,我刚刚发现检查特定值,如 SELECT numR FROM reference WHERE numR =“x”
但我想检查只是如果有一个表上的任何值通过代码

7hiiyaii

7hiiyaii1#

当你在Kullanici_Adi上创建一个唯一的密钥时:

ALTER TABLE Oturum ADD UNIQUE (Kullanici_Adi);

字符串
要替换现有密码,请执行以下操作:

INSERT INTO Oturum VALUES (%s, %s)
ON DUPLICATE KEY UPDATE Sifre=%s


ref用于验证重复密钥更新。
如果您想保留现有值:

INSERT IGNORE INTO Oturum VALUES (%s, %s)


ref表示忽略。
两个语句中受影响的行将指示是否存在现有行。
“check”的问题是它受到竞争条件的影响,另一个web查询可能会在检查和你选择对表采取的任何操作之间运行。所以在编写数据库代码时,考虑竞争条件并执行这些原子性(或在事务中)。

相关问题