Forums Register Login

Passing data from text file to table.

+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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.
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2515 times.
Similar Threads
I am able to display the file information, but I am having problem displaying the employee details.
Reading in .txt files
Not able to send a mail using JAVA code
StringTokenizer
how to convert .csv files into sql server files using java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:16:29.