• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Importing Data from text file into database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to copy data from text file into my database which I created through derby using netbeans. The app keeps running without terminating nor it is showing any error it just keeps running while copying null values in database. Below is the code and I have also attached screenshot of output. Kindly help me with this.


try{
   Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connection = DriverManager.getConnection(urlanddatabasename,userName,password);
if(connection==null)
{
   System.out.println("no connection");
}
else
{
   System.out.println("connection established");
}

    String fileName = "C:\\Users\\MPC\\Desktop\\wslist.txt";
    File file = new File(fileName);
    Scanner inputStream = new Scanner(file);
   
    ps = connection.prepareStatement("INSERT INTO AB.WSLIST"
            +"(SERVICEID, WSDLADDRESS, SERVICEPROVIDER, IPADDRESS, COUNTRY, IPNO, AAS, LATITUDE, LONGITUDE) "
    + "values(?, ?, ?, ?, ?, ?, ?, ?, ?)");
   
    String[] ar = new String[9];    
    while(inputStream.hasNext()){
   
    String idd = ar[0];
    String wsd = ar[1];
    String sp = ar[2];
    String ip = ar[3];
    String c = ar[4];
    String ipn = ar[5];
    String as = ar[6];
    String lat = ar[7];
    String lg = ar[8];
       
    ps.setString(1,idd);
    ps.setString(2,wsd);
    ps.setString(3,sp);
    ps.setString(4,ip);
    ps.setString(5,c);
    ps.setString(6,ipn);
    ps.setString(7,as);
    ps.setString(8,lat);
    ps.setString(9,lg);
     
    int i=ps.executeUpdate();
                   
                   if(i!=0) {                
                       System.out.println("Record inserted");
                       }
                   else{
                       System.out.println("Record not inserted");
                 }
                    connection.commit();
    }
}
finally{ try{
   ps.close();
}
catch (SQLException ex) {
           ex.printStackTrace();
}
  try {
   connection.close();
 } catch(SQLException e) {
   e.printStackTrace();
 }
}
}
Screenshot-2017-11-27-00.31.06.png
[Thumbnail for Screenshot-2017-11-27-00.31.06.png]
 
Marshal
Posts: 27214
87
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here's an excerpt from your code. If you asked me what value would be in the idd variable after that, I would say "An empty string". The same goes for your other variables.

It looks like you forgot the part of the code which gets the data from the file and puts it into the array elements.
 
ss chaudry
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I forgot to add that part. Now I have added that part but error occurs that array index out of bound :/
Below is the new code.Kindly review it ad help me
Screenshot-2017-11-27-16.32.54.png
[Thumbnail for Screenshot-2017-11-27-16.32.54.png]
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start logging some of what's going on.
For example, log the line that is read in, and then log the contents of the array after the split() call (use Arrays.toString() to format it).

As a note, when you get an exception it helps to post the full exception and stack trace, highlighting the line in your code it occurs on.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may or may not be your problem, but here's an improvement to your code that might prevent an index out of bounds error:
What that means is, you are splitting on one or more whitespace characters.
 
ss chaudry
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried both solutions but still same issue :/

Screenshot-2017-11-27-20.24.28.png
[Thumbnail for Screenshot-2017-11-27-20.24.28.png]
 
Dave Tolls
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You really need to be logging things.
At the moment you have no idea what line is causing the issue.

ETA:  That is, what line in the input file.
 
ss chaudry
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everyone for your help. Finally I have removed the error and my code is working fine now.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic