• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing data from text file to table.

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Let's go to the waterfront with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic