Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-2007, 06:50 AM
sania21 sania21 is offline
Newbie
 
Join Date: May 2007
Posts: 29
Rep Power: 6
sania21 is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-26-2007, 12:23 PM
domestic's Avatar   
domestic domestic is offline
Learning Programmer
 
Join Date: Mar 2007
Location: SCOTLAND!
Age: 20
Posts: 39
Rep Power: 7
domestic is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-28-2007, 05:15 AM
sania21 sania21 is offline
Newbie
 
Join Date: May 2007
Posts: 29
Rep Power: 6
sania21 is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-28-2007, 09:32 AM
rafton23 rafton23 is offline
Newbie
 
Join Date: May 2007
Posts: 2
Rep Power: 0
rafton23 is on a distinguished road
Default

===========good====programing=========== =================good====programing============= ===============good====programing================= ===========good====programing===================== =======good====programing======================= =====good====programing=========================== =good====programing============================goo d====programing============================good= ===programing============================good====p rograming============================good====progr aming============================good====programin g============================good====programing=== =========================good====programing======= =====================good====programing=======:con fused:=====================good====programing===== =======================good====programing========= ===================good====programing============= ===============good====programing================= ===========good====programing===================== =======good====programing========================= ===good====programing=========================== =good====programing============================goo d====programing============================good=== =programing============================good====pro graming============================good====program ing============================good====programin g============================good====programing=== =========================good====programing======= =====================good====programing=========== =================good====programing=============== =============good====programing=================== =========good====programing================= ===========good====programing===================== =======good====programing========================= ===good====programing============================g ood====programing============================good= ===programing============================good====p rograming============================good====progr aming============================good====programin g============================good====programing=== =========================good====programing== ===============
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows XP Tricks & Tips!!!!..new ones. pranky Tutorials 9 08-23-2008 04:22 PM
text file manipulations in vb6.0 Ronin_paes Visual Basic Programming 3 06-11-2007 05:54 AM
Linking Text data to deskop njmase JavaScript and CSS 1 01-31-2007 07:33 PM
Working with TADO Table MrDiaz Pascal/Delphi 0 07-03-2006 07:28 PM


All times are GMT -5. The time now is 12:07 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads