-1

I have to parse the file movies.txt which contains the data for 3451 movies. i have to read the movies.txt file and store all the movie data into MySQL database table movies. For each movie, data is collected under 11 columns in movies.txt file.. so how do i do it in java need suggestions

4

2 Answers 2

0

arrays are zero based index and every sql index are 1 based. so just put this in ur code:

try {

        preparedStatement = dbConnection.prepareStatement(insertTableSQL);

        preparedStatement.setString(1, columns[0]);
        preparedStatement.setString(2, columns[1]);
        preparedStatement.setString(3, columns[2]);
        preparedStatement.setString(4, columns[3]);
        preparedStatement.setString(5, columns[4]);
        preparedStatement.setString(6, columns[5]);
        preparedStatement.setString(7, columns[6].replaceAll(",", ""));
        preparedStatement.setString(8, columns[7]);
        preparedStatement.setString(9, columns[8]);
        preparedStatement.setString(10, columns[9]);
        preparedStatement.setString(11, columns[10]);

        //and more inserts....
        // execute insert SQL stetement
        preparedStatement.executeUpdate();

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

    }

I think this is why u got the Interger parse exeption before.

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

Comments

0

Hey there are thousand of ways to do it. I think the best way would creating a Class Movie, loading File and insertering into DB.

But here is an other way to do it.

private void readFile() throws IOException, SQLException{
    File f = new File("C:/Movies.txt");

    BufferedReader in = new BufferedReader(new FileReader(f));
    Connection dbConnection = getConnection();
    String line;

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


    }
    in.close();     
}



private void insertRecordIntoTable(Connection dbConnection,String[] columns) throws SQLException {


    PreparedStatement preparedStatement = null;

    String insertTableSQL = "INSERT INTO MOVIES"
            + "(MOVIE_ID, NAME, AUTHOR, DUNNO) VALUES"
            + "(?,?,?,?)";

    try {

        preparedStatement = dbConnection.prepareStatement(insertTableSQL);

        preparedStatement.setString(1, columns[0]);
        preparedStatement.setString(2, columns[1]);
        preparedStatement.setString(3, columns[2]);
        preparedStatement.setString(4, columns[3]);
        //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();
        }

    }

}

readFile reads ur File and calls the insert Method every line. You also have to create a method (getConnection) to create ur DB connection. If u have any questions just ask :)

17 Comments

in above example you are mentioned to insert each col manually but in my example there more than 3000 movies means more than 3000 rows . its bad idea to insert so many rows manually. i hope u got my problem.
i got ur problem but with 3000 rows there shouldnt be a problem. but u have to call the readFile method just ONE time. so its not really manually. u could also build ur own big insert statement. and after executing it. but i think the time difference wont be that much.
Ya this is good. but they are some columns with multiple values with comma separated so how can i insert those values in single coloumn .
i am getting number format exception for that case ex: java.lang.NumberFormatException: For input string: "comedy,crime,drama"
so some are seperated by , and some by ; ?
|

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.