java.lang.ArrayIndexOutOfBoundsException:11使用以下代码面临此错误

时间:2017-01-11 08:59:17

标签: java database jdbc

这是我实际面临问题的代码。在预准备语句中,值不会在基于零的索引中,但我在列中索引为零的数据。我现在该怎么处理......

public class MovieParsing {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        try {

            File f = new File("D:\\workspace\\CertificationProject\\src\\11_dataset_v1.0.txt");

            BufferedReader in = new BufferedReader(new FileReader(f));
            String username = "root";
            String pwd = "root";
            Class.forName("com.mysql.jdbc.Driver");
            String connurl = "jdbc:mysql://localhost:3306/movies";


            Connection dbConnection = DriverManager.getConnection(connurl, username, pwd);
            String line;

            while ((line = in .readLine()) != null) {
                String[] columns;
                columns = line.split(";");

                insertRecordIntoTable(dbConnection, columns);

                //System.out.println("col1 " + columns[11]);
            } in .close();


            //catch (FileNotFoundException e) { 
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    private static void insertRecordIntoTable(Connection dbConnection,
        String[] columns) throws SQLException {
        // TODO Auto-generated method stub


        PreparedStatement preparedStatement = null;

        String insertTableSQL = "INSERT INTO MOVIES" +
            "(movie_id, movie_title, movie_type, movie_director,movie_imdbrating,movie_runtime,movie_year,movie_ImdbVotes,movie_imdbtop,movie_1001mustsee,movie_url) VALUES" +
            "(?,?,?,?,?,?,?,?,?,?,?)";

        try {

            preparedStatement = dbConnection.prepareStatement(insertTableSQL);

            //  preparedStatement.setString(0, columns[0]);
            preparedStatement.setString(1, columns[1]);
            preparedStatement.setString(2, columns[2]);
            preparedStatement.setString(3, columns[3]);
            preparedStatement.setString(4, columns[4]);
            preparedStatement.setString(5, columns[5]);
            preparedStatement.setString(6, columns[6].replaceAll(",", ""));
            preparedStatement.setString(7, columns[7]);
            preparedStatement.setString(8, columns[8]);
            preparedStatement.setString(9, columns[9]);
            preparedStatement.setString(10, columns[10]);
            //  preparedStatement.setString(11, columns[11]);
            //and more inserts....
            // execute insert SQL stetement
            preparedStatement.executeUpdate();

            System.out.println("Record is inserted into Movies table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (preparedStatement != null) {
                preparedStatement.close();
            }
        }
    }
}

0 个答案:

没有答案