该行是否存在?如果是这样更新它,如果没有添加它mysql / java

时间:2012-08-16 06:20:08

标签: java mysql

public static boolean saveUserInfo(Client c){
try {
    Statement statement = conn.createStatement();
    ResultSet group = statement.executeQuery("SELECT * FROM users WHERE username = '"+ c.playerName + "'");


    if (!group.next())
        statement.execute("INSERT INTO `users` (`username`, `password`, `rights`, `address`, `hasbankpin`, `bankpin1`, `bankpin2`, `bankpin3`, `bankpin4`, `height`, `posx`, `posy`, `cbowcount`, `vls`, `skulltime`, `ep`, `dpoints`, `vlsleft`) VALUES ('"+c.playerName+"', '"+c.playerPass+"', '"+c.playerRights+"', '"+c.getIP()+"', '"+c.hasBankPin+"', '"+c.bankPin1+"', '"+c.bankPin2+"', '"+c.bankPin3+"', '"+c.bankPin4+"', '"+c.heightLevel+"', '"+c.absX+"', '"+c.absY+"', '"+c.crystalBowArrowCount+"', '"+c.degradeTime+"', '"+c.skullTimer+"', '"+c.earningPotential+"', '"+c.dungeonPoints+"', '"+c.vlsLeft+"')");

很抱歉这个烂摊子,但我要做的是检查用户是否存在于表格中。如果是这样,我希望它更新它,如果不是,我希望它添加用户。我一直在尝试这几天,我没有运气。

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:4)

您可以使用ON DUPLICATE KEY UPDATE子句在单个查询中执行此操作:

INSERT INTO table_name(...) 
VALUES(...) 
ON DUPLICATE KEY UPDATE col = value, ...;

您需要在PRIMARY列上添加UNIQUEusername键:

ALTER TABLE users ADD UNIQUE KEY ix1 (username);

答案 1 :(得分:1)

如果你有一个唯一的密钥,比如username,那么你可以这样做:

String query="
    INSERT INTO users SET
        username = ?,
        password = ?,
        rights = ?,
        address = ?,
        hasbankpin = ?,
        bankpin1 = ?,
        bankpin2 = ?,
        bankpin3 = ?,
        bankpin4 = ?,
        height = ?,
        posx = ?,
        posy = ?,
        cbowcount = ?,
        vls = ?,
        skulltime = ?,
        ep = ?,
        dpoints = ?,
        vlsleft = ?
    ON DUPLICATE KEY UPDATE
        password = VALUES(password),
        rights = VALUES(rights),
        address = VALUES(address),
        hasbankpin = VALUES(hasbankpin),
        bankpin1 = VALUES(bankpin1),
        bankpin2 = VALUES(bankpin2),
        bankpin3 = VALUES(bankpin3),
        bankpin4 = VALUES(bankpin4),
        height = VALUES(height),
        posx = VALUES(posx),
        posy = VALUES(posy),
        cbowcount = VALUES(cbowcount),
        vls = VALUES(vls),
        skulltime = VALUES(skulltime),
        ep = VALUES(ep),
        dpoints = VALUES(dpoints),
        vlsleft = VALUES(vlsleft)
";

Statement stmt = conn.prepareStatement(query);                    

stmt.setString(1, c.playerName);
stmt.setString(2, c.playerPass);
stmt.setString(3, c.playerRights);
stmt.setString(4, c.getIP());
stmt.setString(5, c.hasBankPin);
stmt.setString(6, c.bankPin1);
stmt.setString(7, c.bankPin2);
stmt.setString(8, c.bankPin3);
stmt.setString(9, c.bankPin4);
stmt.setString(10, c.heightLevel);
stmt.setString(11, c.absX);
stmt.setString(12, c.absY);
stmt.setString(13, c.crystalBowArrowCount);
stmt.setString(14, c.degradeTime);
stmt.setString(15, c.skullTimer);
stmt.setString(16, c.earningPotential);
stmt.setString(17, c.dungeonPoints);
stmt.setString(19, c.vlsLeft);

stmt.executeUpdate();

答案 2 :(得分:1)

您的代码容易受SQL Injection攻击。为避免这种情况,请使用 JAVA PreparedStatement 。除此之外,它还会让您在数据库中插入具有单引号的记录。 Prepared Statement的一个例子是这样的:

PreparedStatement updateSales = con.prepareStatement(
        "INSERT INTO tableName(colA, colB) VALUES (?, ?)");
updateSales.setInt(1, 75); 
updateSales.setString(2, "Colombian"); 
updateSales.executeUpdate();

请记住始终清理您的输入。

回到你的问题,你可以使用INSERT...ON DUPLICATE KEY UPDATE

示例,(您的用户名必须为UNIQUE

INSERT INTO `users` (`username`, `password`, `rights`, 
                    `address`, `hasbankpin`, `bankpin1`, 
                    `bankpin2`, `bankpin3`, `bankpin4`, `height`, 
                    `posx`, `posy`, `cbowcount`, `vls`, `skulltime`, 
                   `ep`, `dpoints`, `vlsleft`) 
VALUES ('','', .....other values...., '')
ON DUPLICATE KEY 
UPDATE `password` = '', 
       `rights` = '',
       ... other values here