简体   繁体   English

Java-越界异常数据库

[英]Java - Out of Bounds Exception Database

I am practicing Java before the semester starts, and I trying to create code that will read in a specified CSV file, and create a database with it. 我在学期开始之前正在练习Java,并且尝试创建将在指定的CSV文件中读取的代码,并使用它创建数据库。 But I am getting an error that says 但是我得到一个错误,说

"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 “线程“主”中的异常” java.lang.ArrayIndexOutOfBoundsException:3
at LoadData.main(LoadKids.java:52)" 在LoadData.main(LoadKids.java:52)“

My code is listed below. 我的代码在下面列出。
The error is on this line "Rating = fields[3].trim();" 该行的错误是“ Rating = fields [3] .trim();”

import java.io.File;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;


public class LoadData {

public static void main(String[] args) {
    Connection c = null;
    Statement s = null;
    Scanner fromFile = null;
    String sql1 = null, sql2 = null;
    String line = null, ID = null, Console = null, Title = null, Rating = null, Multiplayer = null;
    String[ ] fields;

    try {
        // Define Connection and Statement objects.
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:games.db");
        s = c.createStatement();

        // Instantiate scanner to read from file.
        fromFile = new Scanner(new File ("FileName.csv"));

        // Create the table.
        sql1 = "create table if not exists " +
            "games(gameid integer, " +
            "ID varchar(2), " +
            "Console varchar(15), " +
            "Title varchar(20), " +
            "Rating varchar(2), " +
            "Multiplayer varchar(3));";
        System.out.println("sql1: " + sql1);
        s.executeUpdate(sql1);

        // Read and throw away header line.
        fromFile.nextLine( );

        // Populate the table.
        for (int id = 1001; fromFile.hasNextLine( ); id++) {
            line = fromFile.nextLine( );
            fields = line.split(" ");
            ID = fields[0].trim();
            Console = fields[1].trim();
            Title = fields[2].trim();
            Rating = fields[3].trim();
            Multiplayer = fields[4].trim();
            sql2 = String.format(
                "insert into games (gameid, ID, Console, Title, Rating, Multiplayer) " +
                "values (%d, '%s', '%s', '%s', '%s', '%s');", 
                id, ID, Console, Title, Rating, Multiplayer);
            System.out.println(sql2);
            s.executeUpdate(sql2);
        }
        c.close( );
    }
    catch (FileNotFoundException e) {
        System.out.println("File queries.sql not found.");
        System.err.println( e.getClass().getName() + 
            ": " + e.getMessage() );
    }
    catch(SQLException e) {
        System.out.println("SQLException.");
        System.err.println( e.getClass().getName() + 
            ": " + e.getMessage() );
    }
    catch (ClassNotFoundException e ) {
        System.err.println( e.getClass().getName() + 
            ": " + e.getMessage() );
    }
    finally {
        fromFile.close( );
    }
}
}

Please post the data in your csv file. 请将数据发布到您的csv文件中。 Normally CSV files are separated by commas, hence the name comma separated values. 通常,CSV文件用逗号分隔,因此名称用逗号分隔。 However, you code is using a space to separate the values. 但是,您的代码使用空格分隔值。

fields = line.split(" ");

If your CSV data is really separated by commas, change that line to 如果您的CSV数据确实用逗号分隔,请将该行更改为

fields = line.split(",");

If your CSV data is separated by spaces, verify there are at least 5 items separated by spaces on every line in the file. 如果您的CSV数据用空格分隔,请确认文件中的每一行至少有5个项目用空格分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM