多次插入SQL数据库

时间:2016-06-03 23:03:24

标签: java mysql sql-server sql-insert

我正在创建一个代码生成器,它生成一个新代码,然后查询数据库以查看它是否存在。如果是,请再次尝试制作不同的代码。如果它不存在,请将其添加到数据库中。但是当我将一个代码添加到数据库中时,查询会添加3个不同的行,其中包含3个不同的值。其中一个值是应该添加的值,另外两个我不知道它们来自哪里。当我只设置添加一个时,为什么插入3。我的完整类文件是:

package com.xium.accesscode;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ThreadLocalRandom;

import com.xium.log.ServerLogger;
import com.xium.sql.DBConnections;
import com.xium.utils.StringUtils;

public class NewAccessCode {

static String AccessCodeDBuser = "root";
static String AccessCodeDBpass = "";
static String AccessCodeDBhost = "localhost";

static String newAccessCode;
static String randS;
static String randFinal;

static int min = 000000000;
static int max = 999999999;
static int randI;

public static void AccessCode() {
    if(newAccessCode() == 0) {
        ServerLogger.writeLog("[ALERT] Database Error");
    } else if(newAccessCode() == 1) {
        //Reruns the code generator, to make a unique code
        newAccessCode();
    } else if(newAccessCode() == 2) {
        ServerLogger.writeLog("[NOTE] New Access Code: " + newAccessCode);
    }
}

/*
 * Return Codes:
 * 0 - Database Error
 * 1 - Code Already Exists
 * 2 - New Access Code Added
 */

private static int newAccessCode() {
    genAccessCode();
    newAccessCode = randFinal;

    //Does it already exist?
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet results = null;
    String statement = "SELECT count(*) FROM `xium`.`accesscodes` WHERE `accesscode`='" + newAccessCode + "'";
    String statement2 = "INSERT INTO `xium`.`accesscodes` (`accesscode`, `used`, `assignedto`) VALUES ('" + newAccessCode + "', '0', '')";

    try {
        connection = DBConnections.getAccessCodeDB(AccessCodeDBuser, AccessCodeDBpass, AccessCodeDBhost);
        preparedStatement = connection.prepareStatement(statement);
        results = preparedStatement.executeQuery();

        results.next();

        if(results.getInt(1) == 0) {

        } else if(results.getInt(1) >= 1) {
            return 1;
        }

        connection = DBConnections.getAccessCodeDB(AccessCodeDBuser, AccessCodeDBpass, AccessCodeDBhost);
        preparedStatement = connection.prepareStatement(statement2);
        preparedStatement.executeUpdate();
        return 2;
    } catch (SQLException e) {
        return 0;
    }
}

private static String genAccessCode() {
    randI = ThreadLocalRandom.current().nextInt(min, max + 1);
    randS = randI + "";
    randFinal = StringUtils.toMD5(randS);
    return randFinal;
}

}

3 个答案:

答案 0 :(得分:0)

在AccessCode()静态方法中调用newAccessCode()方法三次。 将其更改为

public static void AccessCode() {
int newAccessCodeReturn = newAccessCode();
    if(newAccessCodeR`enter code here`eturn  == 0) {
        ServerLogger.writeLog("[ALERT] Database Error");
    } else if(newAccessCodeReturn  == 1) {
        //Reruns the code generator, to make a unique code
        newAccessCode();
    } else if(newAccessCodeReturn  == 2) {
        ServerLogger.writeLog("[NOTE] New Access Code: " + newAccessCode);
    }
}

答案 1 :(得分:0)

您在newAccessCode()代码中反复拨打if/else if。每次执行此操作都会插入到DB中。调用一次并将结果保存在变量中。

int result = newAccessCode();
if(result == 0) {
    ServerLogger.writeLog("[ALERT] Database Error");
} else if(result == 1) {
    //Reruns the code generator, to make a unique code
    newAccessCode();
} else if(result == 2) {
    ServerLogger.writeLog("[NOTE] New Access Code: " + newAccessCode);
}

或使用switch声明:

switch (newAccessCode()) {
    case 0:
        ServerLogger.writeLog("[ALERT] Database Error");
        break;
    case 1:
        //Reruns the code generator, to make a unique code
        newAccessCode();
        break;
    case 2:
        ServerLogger.writeLog("[NOTE] New Access Code: " + newAccessCode);
        break;
}

答案 2 :(得分:0)

每次运行AccessCode()函数时,if语句也会运行该语句。所以不要这样做:

if(newAccessCode() == 0)

您应该将新的整数值设置为等于newAccessCode()函数的值,然后检查int的值。

所以:

int returnValue = newAccessCode();

然后检查returnValue的值。

if(returnValue == 0)

那应该解决你的问题。