Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to solve ArrayIndexOutOfBoundsException in Java?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 18
    Answer it

    I want to insert a csv data into a MySQL table. When I run the code, I've got the following error message. Any suggestions?

    java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
        at BudgetTrackerCui/CSVInsert.CSVInsert.main(CSVInsert.java:44)

     

    package CSVInsert;
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    
    import static java.lang.Integer.parseInt;
    
    public class CSVInsert {
        public static void main(String[] args) {
            String jdbcUrl="jdbc:mysql://192.168.0.45:3306/testdb";
            String username="user_name";
            String password="password";
    
            String filePath="/home/user/Documents/GDP.csv";
    
            int batchSize=20;
    
            Connection connection=null;
    
    
            try{
                connection= DriverManager.getConnection(jdbcUrl,username,password);
                connection.setAutoCommit(false);
    
                String sql="insert into gdp_table(country_code,ranking,country_name,gdp) values(?,?,?,?)";
    
                PreparedStatement statement=connection.prepareStatement(sql);
    
                BufferedReader lineReader=new BufferedReader(new FileReader(filePath));
    
                String lineText=null;
                int count=0;
    
                lineReader.readLine();
                while ((lineText=lineReader.readLine())!=null){
                    String[] data=lineText.split(",");
    
                    String country_code=data[0];
                    String ranking=data[1];
                    String country_name=data[2];
                    String gdp=data[3];
    
                    statement.setString(1,country_code);
                    statement.setInt(2,parseInt(ranking));
                    statement.setString(3,country_name);
                    statement.setInt(4,parseInt(gdp));
                    statement.addBatch();
                    if(count%batchSize==0){
                        statement.executeBatch();
                    }
                }
                lineReader.close();
                statement.executeBatch();
                connection.commit();
                connection.close();
                System.out.println("Data has been inserted successfully.");
    
            }
            catch (Exception exception){
                exception.printStackTrace();
            }
    
        }
    }

    Here is a sample of the csv file.

    country_code ranking country_name gdp
    USA 1 United States 20936600
    CHN 2 China 14722731
    JPN 3 Japan 5064873
    DEU 4 Germany 3806060
    GBR 5 United Kingdom 2707744
    IND 6 India 2622984
    FRA 7 France 2603004
    ITA 8 Italy 1886445
    CAN 9 Canada 1643408

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: