aspose file tools*
The moose likes Java in General and the fly likes Passing data from text file to table. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Passing data from text file to table." Watch "Passing data from text file to table." New topic
Author

Passing data from text file to table.

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok here is my problem in the below shown program i am trying to pass data from txt file to a table,but i get error on StringTokenizer method(nexttoken)i am unable to get the output please if anybody can solve my problem of what is actualy wrong with this 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("jdbc dbc: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 + "\"");
}

}
Thanks in advance!!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13881
    
  10

What is the error that you are getting? Is it a compiler error or an exception at runtime? If so, what is the exception? Did you try to understand the error message?

If you want to parse a string into parts delimited with "/", then String.split() is probably an easier solution than using StringTokenizer.

Also, consider using PreparedStatement instead of concatenating SQL statements like you are doing in your code.

This kind of code is vulnerable to SQL injection.
[ May 23, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
hi jesper!
Now i am getting more confused because first when i was running the program i was getting the error at line tk.nextToken("/"),and it was showing error "no suggestions avaliable".Now when i am running the program i am getting the following error.Now what is the problem with driver?

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ';'.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:253)
at client.com.kolkata.Read.ReadingText.main(ReadingText.java:38)
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok,i tried to locate the main error its coming on the statement
"stmt.executeQuery(query);"
As the query is not getting executed,my program is not giving the output.
There is no error of driver.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing data from text file to table.
 
Similar Threads
I am able to display the file information, but I am having problem displaying the employee details.
Not able to send a mail using JAVA code
Reading in .txt files
StringTokenizer
how to convert .csv files into sql server files using java