Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Write Text File to Table 
 

In this section, you will learn how to read the records of a simple text file and write (insert) into a simple table in MySQL database. All records are written in the simple text file and again, if you need to insert these records in tabular format the fo

 

Write Text File to Table

                         

In this section, you will learn how to read the records of a simple text file and write (insert) into a simple table in MySQL database. All records are written in the simple text file and again, if you need to insert these records in tabular format the following example will provide the facility for writing this content.

Description of program:

The following program helps you in writing the records in the MySQL database table  from the simple text file. For this, you must have to create a simple text file that have to be inserted the records into a database table. This program uses the simple Text file (Employee_list.txt) that contains all employees record (id, Emp_name, Emp_depart and sal). After creating a text file, you should establish the connection with MySQL database. Here, we applying the connection() method that will provide the connection and another is TextFileToTable() method that helps you for writing or inserting the records into a MySQL database table (Employee_Records). The inserting process is completely successfully, it will show a message "All data are inserted in the database table".  

Description of code:

StringTokenizer(String data, String comma):
This is the constructor of StringTokenizer class that allows the permission for breaking an application into tokens. It constructs a string tokenizer for the given string. The characters are separated by the comma arguments. 

nextToken():
This is the method that returns the next tokens from the StringTokenizer object. 

Here is the code of program:

import java.sql.*;
import java.io.*;
import java.util.*;

public class TextFileToTable{
  Connection con = null;
  Statement st;
  public static void main(String[] args) {
    System.out.println("Write Text File to Table!");
    TextFileToTable text = new TextFileToTable();
  }

  public Connection connection(){
    try{
      Class.forName("com.mysql.jdbc.Driver");
    }
    catch(ClassNotFoundException c){
      System.out.println("Class not found!");
    }
    try{
      con = DriverManager.getConnection(
          "jdbc:mysql://localhost:3306/jdbctutorial"
,"root","root");
    }
    catch(SQLException s){
      System.out.println("Connection is not found!");
    }
    return con;
  }

  public TextFileToTable(){
    try{
      FileInputStream fstream = new FileInputStream("Employee_list.txt");
      DataInputStream dstream = new DataInputStream(fstream);
      BufferedReader bf = new BufferedReader(new InputStreamReader(dstream));
      String data = null;
      String comma = ",";
      while((data = bf.readLine()) != null){
        StringTokenizer stoken = new StringTokenizer(data,comma);      
        String Emp_id = stoken.nextToken();
        int id = Integer.parseInt(Emp_id);
        String Emp_name = stoken.nextToken();
        String Emp_depart = stoken.nextToken();
        String Emp_sal = stoken.nextToken();
        int sal = Integer.parseInt(Emp_sal);
        st = connection().createStatement();
        int row = st.executeUpdate("INSERT Employee_Records VALUES
               ("
+id+" , '"+Emp_name+"'"+" , '"+Emp_depart+"' ,"+sal+")");
      }
      System.out.println("All data are inserted in the database table");
      bf.close();
      st.close();
    }
    catch(Exception e){
      e.printStackTrace();
    }
  }
}

Download this example.

                         

» View all related tutorials
Related Tags: c database query ide table data io column numbers number sum using this id simple tab student university if ie

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

This is very much useful for me
thanku

Posted by EJILARASSI on Thursday, 06.5.08 @ 15:26pm | #62182

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.