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.


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

package StringTokenizer;

import db.Insert;
import db.Query;
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()];;
         StringTokenizer str=new StringTokenizer(new String(b), “\n”);
         int i=1;
      //   Query q=new Query();
         Insert in=new Insert();
             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.