Closed Thread
Results 1 to 4 of 4

Thread: Program to pass data from text file to table.

  1. #1
    sania21 is offline Newbie
    Join Date
    May 2007
    Posts
    29
    Rep Power
    0

    Question Program to pass data from text file to table.

    I m a bit confused with this program.I am getting error with StringTokenizer method nextToken(); ,but unable to solve this error.
    In a text file Records i have saved the data in the format shown below

    /First/Sania/Last/Arora....etc


    My java program is as below


    public class ReadingText {
    public static final String Records="Records";
    public static void main(String[] args) throws NoSuchElementException {

    try
    {


    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn=DriverManager.getConnection("jdbcdbc:sap11","sa","");
    Statement stmt=conn.createStatement();

    BufferedReader in=new BufferedReader(new FileReader("E:\\Records.txt"));
    String line=in.readLine();

    while(line!=null){
    StringTokenizer tk=new StringTokenizer(line);
    String First=tk.nextToken("/"),Last=tk.nextToken("/"),Email=tk.nextToken("/"),Phone=tk.nextToken("/");


    String query="insert into "+ Records;
    query+="values(" + quote(First)+ "," ;
    query+=quote(Last)+ ",";
    query+=quote(Email)+ ",";
    query+=quote(Phone)+ "); ";

    stmt.executeQuery(query);


    line=in.readLine();

    }

    in.close();

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }


    private static String quote(String include) {

    return("\"" + include + "\"");
    }

    }


    the error does not explain anything it just gets pointed when i try to run.
    Please if any one can explain what is the error in this program why its not giving desired output.it will be realy kinda of you .Thanks in advance.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    domestic's Avatar
    domestic is offline Learning Programmer
    Join Date
    Mar 2007
    Location
    SCOTLAND!
    Posts
    44
    Rep Power
    0

    This should work....

    Um, I personally use a different method, using a custom java library thingy which if u want im pretty sure i could give to you. However, I think this should work...

    Code:
    \\Create BufferedReader called reader and give it the filename
    BufferedReader reader = new BufferedReader("FileNameHere.txt");    
    
    \\Read data temporarily assigned to variable "data"
            String data = reader.readLine();  
            while (data != null) {     
    
    \\If data is split up by a symbol ie tab, then insert symbol here. ("\t"=tab)
    \\Each "bits" is assigned to the appropriate String STARTING AT 0.
    
                String[] bits = data.split("\t"); 
                String registration = bits[0];
                String type = bits[1];
                String costs = bits[2];
    
    \\Then procceeds to read next line in file
                data = reader.readLine();      
            }
    
    \\Once all done, closes reader
            reader.close();
    
    \\Prints to terminal "Finished", just to let u know. It is optional.
            System.out.println("Reader has finished");
        }
    Let me know how it goes

    Domestic

    PS: I havent tested it - my proper PC is on the fritz and under repairs.....(its in pieces on my floor)
    .


    Programming Languages: Java, VB6, VB2005 (.NET2)
    Web Languages: HTML, CSS, JS

    Website: http://abdn.ac.uk/~u41am6

    Opportunity is missed by most people because it is dressed in overalls and looks like work.

  4. #3
    sania21 is offline Newbie
    Join Date
    May 2007
    Posts
    29
    Rep Power
    0

    Unhappy Tried this but again a error..

    Thank u for replying .
    I modified my program by using the split method but still i get a error.
    The error does not explain anything it just gets pointed.Please refer to the below program.



    public class ReadingText {
    public static final String Records="Records";
    public static void main(String[] args) throws NoSuchElementException {

    try
    {


    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    Connection conn=DriverManager.getConnection("jdbcdbc:sania","sa","");



    BufferedReader in=new BufferedReader(new FileReader("E:\\Records.txt"));
    String line=in.readLine();

    while(line!=null){

    String[] bits = line.split("\t");
    String First = bits[0];
    *(Error at this line)* String Last = bits[1];
    String Email = bits[2];
    String Phone = bits[3];

    System.out.println("ok");

    String query="insert into Records(First,Last,Email,Phone)values("+quote(Firs t)+","+quote(Last)+","+quote(Email)+" , "+quote(Phone)+" )";

    PreparedStatement pstmt=conn.prepareStatement(query);

    pstmt.executeQuery(query);


    line = in.readLine();

    }

    in.close();

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }


    private static String quote(String include) {

    return("\"" + include + "\"");
    }

    }



    one more doubt to be asked is in whta exact format should i save the data in my txt file?
    The format which i use is as below.
    /First/sonu/Last/Marthur/Email/sonumarthar/Phone/264567

    I am unable to track the error please can anyone explain.
    Thanks in advance.

  5. #4
    rafton23 Guest
    ===========good====programing===================== =======good====programing============================good====pro graming============================good====program ing============================good====programing============================good== ==programing============================good====pr ograming============================good====progra ming============================good====programing=========== =================good====programing=============== =============good====programing=================== =========good====programing======================= =====good====programing=========================== =good====programing============================goo d====programing============================good====programing=========== =================good====programing=============== =============good====programing=================== =========good====programing======================= =====good====programing=========================== =good====programing============================goo d====programing============================good====programing=== =========================good====programing======= =====================good====programing=========== =================good====programing=============== =============good====programing============================good====programing================== ==========good====programing====================== ======good====programing========================== ==good====programing============================go od====programing============================good== ==programing============================good====pr ograming============================good====programing================= ===========good====programing===================== =======good====programing========================= ===good====programing============================g ood====programing============================good= ===programing============================good====p rograming============================good====progr aming============================good====programin g============================good====programing=================

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 12-26-2011, 01:05 PM
  2. [C#] how to read data from a text file
    By Crock in forum Classes and Code Snippets
    Replies: 0
    Last Post: 07-05-2011, 03:35 AM
  3. Load / save data from text file
    By wazofski in forum Visual Basic Programming
    Replies: 20
    Last Post: 11-14-2010, 10:10 AM
  4. Replies: 15
    Last Post: 07-27-2010, 01:27 PM
  5. exporting database (table) to a text file.
    By Str1k3z in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-19-2009, 07:36 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts