将Arraylist插入Derby Database表

时间:2014-04-17 15:42:34

标签: java sql arraylist derby

您好我在下面的数据列表中插入数据列表是我的插入表方法:

   private static void insertPerson(String id, String firstName, String lastName, String Street, String City) {
    try {
        stmt = (Statement) conn.createStatement();
        stmt.execute("insert into " + tableName + " values (" + id + ",'" + firstName + "','" + lastName + "','" + Street + "','" + City + "')");
        stmt.close();
    } catch (SQLException sqlExcept) {
        sqlExcept.printStackTrace();
    }
}

我正在尝试使用以下代码insert into database

ArrayList PersonList = new ArrayList(Arrays.asList(formatedString.split(",")));
    for (int i = 0; i < PersonList.size(); i++) {
    String []value=formatedString.split(",");
   insertPerson(value[0], value[1], value[2], value[3], value[4]);
   System.out.println(PersonList.toString());
  }

我收到以下错误:

线程AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1

中的异常

任何建议都会非常感谢。

2 个答案:

答案 0 :(得分:0)

这可能是问题

// 1. Split `formatedString` as a List of Persons by `,`
ArrayList PersonList = new ArrayList(Arrays.asList(formatedString.split(",")));
// 2. Walk through the list of persons
for (int i = 0; i < PersonList.size(); i++) {
    // 3. Split the same `formatedString` in fields ?!?
    // What should happen here?
    // II think this is your problem        
    String []value=formatedString.split(",");
    insertPerson(value[0], value[1], value[2], value[3], value[4]);
    System.out.println(PersonList.toString());
}

<强>更新

包含数据的工作示例(人员之间的换行符 - 字段之间的空格)

public class Splitter {

    public static void main(String[] args) {
        String values = "1 Ola Hansen Timoteivn Sandnes\n"
                + "2 Tove Svendson Borgvn Stavanger\n"
                + "3 Kari Pettersen Storgt Stavanger";
        String[] rows = values.split("\n");
        for (String row : rows) {
            String[] fields = row.split(" ");
            System.out.println(String.format("id: %s firstname: %s lastname: %s street: %s city %s", fields[0], fields[1], fields[2], fields[3], fields[4]));
        }
    }
}

更新2

此示例从文件(/tmp/test.txt

中读取数据
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;


public class Splitter {

    public static void main(String[] args) throws FileNotFoundException, IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("/tmp/test.txt")));
        String row;
        while ((row = input.readLine()) != null) {
            String[] fields = row.split(" ");
            System.out.println(String.format("id: %s firstname: %s lastname: %s street: %s city %s", fields[0], fields[1], fields[2], fields[3], fields[4]));
        }
    }
}

该文件看起来像这样

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

答案 1 :(得分:0)

ArrayIndexOutOfBoundsException:1表示你的split(String [] value = formatedString.split(“,”);)不起作用,没有任何值[1] - 值[4]。确保formatedString中有4个逗号,或者在执行拆分之前尝试使用System.out.println(formatedString)来查找导致异常的内容。