It's not a secret anymore!
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

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 {


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();

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)+ "); ";





catch(Exception e)

private static String quote(String include) {

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

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

Joined: Aug 16, 2005
Posts: 15092

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 8 API documentation
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(
at sun.jdbc.odbc.JdbcOdbc.standardError(
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(
at sun.jdbc.odbc.JdbcOdbcStatement.execute(
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
ok,i tried to locate the main error its coming on the statement
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:
subject: Passing data from text file to table.
It's not a secret anymore!