Read Line by Line from file and Insert it into Database Table using StringTokenizer

This example demonstrates how line by line can be read from a file and inserted into database table.

Wherever the pointer find \n or gets to the end of line a token of string i.e. a line is returned by the StringTokenizer. Compile and go through the program to understand whats going on…

Please follow the previous posts to to find the following codes or java programs which have been used here too for creating database connection, creating table and making query to database table.

1. DBConn.java
2. CreateTable.java
3. Insert.java
4. Query.java
 5. Read.java

These programs can be found from here… or browse through the blog to find suitable codes for your program. 

6. RLIL.java
package StringTokenizer;

import db.Insert;
import db.Query;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.StringTokenizer;

/**
 *
 * @author Sudhanshu
 */
public class RLIL {

    /**
     * @param args the command line arguments
     */
  public static void main(String[] args) throws IOException {
        
         FileInputStream fis=new FileInputStream(“E://core//io//NB//myfile.txt”);
         byte[] b=new byte[fis.available()];
         fis.read(b);
        
         StringTokenizer str=new StringTokenizer(new String(b), “\n”);
        
         int i=1;
      //   Query q=new Query();
         Insert in=new Insert();
        
         while(str.hasMoreTokens()){
             in.insert(i++, str.nextToken());
         }
        
        
    }
}

Note.: Please make sure you have already installed mysql server and you have mysql connector reference in classpath before compiling the programs.