0

this is the code where i am actually facing issues. In prepared statement values wont be in zero based index but i have the data with zero based indexed in column. how do i deal with this now...

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();
            }
        }
    }
}
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
kps
  • 3
  • 7
  • 3
    er `preparedStatement.setString(1, columns[0]); preparedStatement.setString(2, columns[1]);` etc etc – Scary Wombat Jan 11 '17 at 09:00
  • 1
    Arrays in Java are zero-based, i.e. use `columns[0]` through `columns[9]`, accessing `columns[10]` is probably what is causing the error. – Tim Biegeleisen Jan 11 '17 at 09:01
  • So `columns = line.split(";")` doesn't return a large enough array. Recheck what `line` is when your exception occurs. – Tom Jan 11 '17 at 09:07
  • i did as mentioned but facing this error....Parameter index out of range (11 > number of parameters, which is 10). – kps Jan 11 '17 at 09:24

0 Answers0