只有数组的第一行被添加到数据库Java中

时间:2014-04-18 19:52:22

标签: java arrays database

我正在尝试将数组添加到数据库中。我可以将第一行放入数据库,但它不会添加剩余的行。

这是值数组字符串:

  

[1,Ola,Hansen,Timoteivn,Sandnes,2,Tove,Svendson,Borgvn,Stavanger,3,Kari,Pettersen,Storgt,Stavanger]

这是我要添加的代码:

try {
        BufferedReader br = new BufferedReader(new FileReader(
                "newoutfile.txt"));
        // String line;
        for (String line = br.readLine(); line != null; line = br
                .readLine()) {
            String newline = line.substring(2);
            String newline2 = newline.trim();
            String[] value = newline2.split(",");

            System.out.println("the array" + Arrays.toString(value));

            connection.createStatement().execute(
                    "insert into person10(personid,first_name,last_name,street,city)values('"
                            + value[0] + "','" + value[1] + "','"
                            + value[2] + "','" + value[3] + "','"
                            + value[4] + "')");
        }

我整天都在拉着头发试图让它发挥作用。任何帮助都会很棒。谢谢。

2 个答案:

答案 0 :(得分:1)

您的值数组字符串(输入文件)应如下所示:

[1, Ola, Hansen, Timoteivn, Sandnes] 
[2, Tove, Svendson, Borgvn, Stavanger]
[3, Kari, Pettersen, Storgt, Stavanger]

然后阅读器和while(或for)循环将处理读取每一行。剥离前导记录号,根据","拆分字符串,然后执行createStatement。

如果您的值在文件中全部在一行中:

[1, Ola, Hansen, Timoteivn, Sandnes , 2, Tove, Svendson, Borgvn, Stavanger , 3, Kari, Pettersen, Storgt, Stavanger]

然后,您需要更多逻辑将该行分成3组独立的数据。

如果文件中有三个单独的数据行(如顶部所示)(而不是一行数据),那么此代码应该有效。

try {
    BufferedReader br = new BufferedReader(new FileReader( "newoutfile.txt"));
    // String line;
    while( ( line = br.readLine() ) != null ) {
        String[] value = line.trim().split(",");

        System.out.println("the array" + Arrays.toString(value));

        connection.createStatement().execute(
                "insert into person10(personid,first_name,last_name,street,city)values('"
                        + value[0] + "','" + value[1] + "','"
                        + value[2] + "','" + value[3] + "','"
                        + value[4] + "')");
    }

如果所有数据都在文件中的一行上,那么就是这样的。

try {
    BufferedReader br = new BufferedReader(new FileReader( "newoutfile.txt"));
    // String line;
    while( ( line = br.readLine() ) != null ) {
        String[] value = line.trim().split(",");

        System.out.println("the array" + Arrays.toString(value));
        int rows = ( value.length / 5);
        for ( int i = 0; i < rows; i++) {
           String personid = value[ i * 5 ];
           String first_name = value[ i * 5  + 1 ];
           String last_name = value[ i * 5 + 2 ];
           String street = value[ i * 5  + 3 ];
           String city = value[ i * 5  + 4 ];
           connection.createStatement().execute(
                "insert into person10(personid,first_name,last_name,street,city)values('"
                        + personid + "','" + first_name + "','"
                        + last_name + "','" + street + "','"
                        + city + "')");
        }
    }

我确定有人可以改进那个丑陋的循环,但是让我们称之为#34;足够好&#34;。 没有变量personid,first_name,last_name,street,city:     尝试{         BufferedReader br = new BufferedReader(new FileReader(&#34; newoutfile.txt&#34;));         //字符串行;         while((line = br.readLine())!= null){             String [] value = line.trim()。split(&#34;,&#34;);

        System.out.println("the array" + Arrays.toString(value));

        connection.createStatement().execute(
                "insert into person10(personid,first_name,last_name,street,city)values('"
                        + value[0] + "','" + value[1] + "','"
                        + value[2] + "','" + value[3] + "','"
                        + value[4] + "')");
    }

如果所有数据都在文件中的一行上,那么就是这样的。

try {
    BufferedReader br = new BufferedReader(new FileReader( "newoutfile.txt"));
    // String line;
    while( ( line = br.readLine() ) != null ) {
        String[] value = line.trim().split(",");

        System.out.println("the array" + Arrays.toString(value));
        int rows = ( value.length / 5);
        for ( int i = 0; i < rows; i++) {
           connection.createStatement().execute(
                "insert into person10(personid,first_name,last_name,street,city)values('"
                        + value[ i * 5 ] + "','" + value[ i * 5  + 1 ] + "','"
                        + value[ i * 5 + 2 ] + "','" + value[ i * 5  + 3 ] + "','"
                        + value[ i * 5  + 4 ] + "')");
        }
    }

答案 1 :(得分:0)

而不是for loop试试这个

while((line = (br.readLine())) != null) {
   System.out.println(line);
}

编辑(试试这个):

String line = br.readLine(); //read the line in a string
String[] strArray = line.split("[\\d],"); //split a string with regex = integer followed by a comma

for (int i = 1; i < strArray.length; i++) { //start from 1 because zero index is empty
   String newline = strArray[i].substring(1);
   String newline2 = newline.trim();
   String[] value = newline2.split(",");
   System.out.println("the array" + Arrays.toString(value));
}