简体   繁体   English

java打印出一个arraylist来构建一个SQL字符串

[英]java printing out an arraylist to build a sql sting

I am trying to build a SQL statement using a set of values from an array list. 我正在尝试使用数组列表中的一组值来构建SQL语句。 I'll start from the beginning. 我将从头开始。

Define my arrayList 定义我的arrayList

ArrayList<String[]> myRecords = new ArrayList<String[]>();

Define my array string: 定义我的数组字符串:

String[] record = new String[2];

Then i loop though a file and split on on the comma and add the record to myRecords 然后我遍历一个文件并在逗号上分割并将记录添加到myRecords

while(scan.hasNext())
        {
            record = scan.nextLine().split(",");
            myRecords.add(record);
        }

So now I need to create an SQL import statement and use thoes values in the arraylist. 所以现在我需要创建一个SQL导入语句,并在arraylist中使用thoes值。 For example. 例如。

String sql = "insert into table (parm1, parm2) Values ";

Now I want to loop though the array and put fill in param1 & param2. 现在,我想循环遍历数组,并填写param1&param2。

   for(String[] list : myRecords)
    {
       sqlString +=("(");
        for(String list1 : list)
        {
            sqlString +=("'"+ list1+ "',");
        }
        sqlString+=("),");
    }
        sqlString += ';';

    logger.info(sqlString);

The problem now is when I print out the SQL string it looks like this. 现在的问题是,当我打印出SQL字符串时,它看起来像这样。

insert into table (parm1, parm2) Values ('value1','value2',),('value3','value4',),;

So on and so forth. 等等等等。

So the problem is that there is an extra comma in there at the end of value2 and value4. 因此,问题在于value2和value4的末尾有一个多余的逗号。 And after the last bracket before the ; 并且在最后一个括号之后; I've gone switching it around etc, but I can't seem to figure out how i can get a proper formatted sql string. 我已经在其他地方切换了,但是我似乎无法弄清楚如何获得正确格式的sql字符串。

This is how it should look. 这就是它的外观。

 insert into table (parm1, parm2) Values ('value1','value2'),('value3','value4');

Perhaps it's the way I am forming my for loop. 也许这就是我形成for循环的方式。 Any suggestions would be great! 任何建议都很好!

Add a counter and initialize it to 0, increment the counter after added a value. 添加一个计数器并将其初始化为0,在添加值后递增计数器。 Only append the comma when the counter is larger than 0 仅当计数器大于0时才添加逗号

int recCount = 0;
for(String[] list : myRecords)
{
    if (recCount > 0)
    {
        sqlString += ",";
    }
    sqlString +=("(");
    int count = 0
    for(String list1 : list)
    {
        if (count > 0)
        {
            sqlString += ",";
        }
        sqlString += "'"+ list1+ "'";
        count++;
    }
    sqlString+=(")");
    recCount++;
}

Building SQL strings in general is not a great idea and is a major security risk 通常,构建SQL字符串不是一个好主意,并且是主要的安全风险

Simply: 只是:

boolean first = true;
for(String[] list : myRecords) {
    if(first) {
        first = false;
    } else {
        sqlString += ',';
    }
    sqlString +=("(");
    for(String list1 : list) {
        sqlString +=("'"+ list1+ "',");
    }
    sqlString+=(")");   // no more "'"
}
sqlString += ';';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM