Pregunta

Hi I am having problems inserting a array list into a derby database below is my insert to table method:

   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();
    }
}

And I am trying to insert into database with the following code:

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());
  }

I am getting the following error:

Exception in thread AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1

Any suggestion would be great thanks.

¿Fue útil?

Solución

This could be the problem

// 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());
}

UPDATE

A working example with your data (linebreaks between the persons - spaces between the fields)

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]));
        }
    }
}

UPDATE 2

This example reads the data from a file (/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]));
        }
    }
}

The file looks like this

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

Otros consejos

The ArrayIndexOutOfBoundsException: 1 means your split (String []value=formatedString.split(",");) isn't working, there isn't anything in value[1] - value [4]. Make sure whatever is in formatedString has 4 commas in it, maybe try System.out.println(formatedString) before you do the split to find what's causing the exception.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top