1

I have a code which reads data from a file and inserts it into database.

this code writes the data into a file.

public void save(Collection<Book> b) {
    try (PrintWriter print = new PrintWriter(this.file);) {
        for (Book book : b) {
            String str = book.getName() + "," + book.getAuthor() + ","
                    + book.getDate() + "\n";
            print.println(str);
        }
    } catch (Exception e) {
    }

}

this code writes the data from the a file and inserts it into db.

try(Reader reader = new FileReader(this.file);
            BufferedReader br = new BufferedReader(reader)) {
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/myBook", this.userName,
                this.pass);

        Statement statement = connection.createStatement();
        String str;          
        while((str = br.readLine()) != null){

            String[] array = str.split(",");
            statement.executeUpdate("Insert Into myBook.book (name,author,pubDate) values('"+array[0]+"', '"+ array[1]+"', '"+array[2]+"')");
        }

    } 

But it throws Exception

java.lang.ArrayIndexOutOfBoundsException

What is wrong?

12
  • because array has only one element Commented Apr 1, 2015 at 14:05
  • The input doesn't contain ",". It should have two or more to be able to use array[1] and array[2]. Commented Apr 1, 2015 at 14:06
  • no it contains definitely thees are my rows in the file Chenty,Rafi,01.25.1850 Kaytser,Rafi,01.25.1855 Commented Apr 1, 2015 at 14:07
  • Have you tried debugging? Commented Apr 1, 2015 at 14:09
  • 1
    Please add any clarification to your question itself (using the edit link) - not in the comments please. Commented Apr 1, 2015 at 14:23

1 Answer 1

2

Do the following:

  1. check the content of the file, how many ',' char does it have in each line, it should have at least 2
  2. check the size of the array, it should be at least 3 in each read iteration, since you are accessing array[2]

It seems that the array at a certain line or iteration doesn't have 3 items, maybe one or two.

example:

line 1: aaa,bbb,ccc   //2 ',' => array={"aaa","bbb","ccc"} this is fine
line 2: ddd,eee       //1 ',' => array={"ddd","eee"} this causes the
                      // exception since array[2] does not exist NULL
line 3: empty         //0 ',' => array={} this causes the exception

If you are not sure of what is happening run the following code:

    try(Reader reader = new FileReader(this.file);
            BufferedReader br = new BufferedReader(reader)) {
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/myBook", this.userName,
                this.pass);

        Statement statement = connection.createStatement();
        String str;          
        while((str = br.readLine()) != null&&str.split(",").length>=3){

            String[] array = str.split(",");
            statement.executeUpdate("Insert Into myBook.book 
(name,author,pubDate) values('"+array[0]+"', '"+ array[1]+"', '"+array[2]+"')");
        }

    }

If the above code run without error then at least one line doesn't have 2 ',' chars if the application still firing the same error then it will be something else.

Sign up to request clarification or add additional context in comments.

1 Comment

it contains 2 ',' chars and i checked the array, its size is 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.