Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:22:55+00:00 2026-05-27T12:22:55+00:00

I am very new to Java. I have a task with two steps. I

  • 0

I am very new to Java. I have a task with two steps.

  1. I want to read all data from a .csv file.
  2. After reading that data I have to put it into a SQL Server database.

I have done step one. I’m able to read the .csv file data, but I don’t know that how to insert it into a database.

Here’s my code for fetching the .csv file data:

import java.io.BufferedReader;  
import java.io.FileNotFoundException;  
import java.io.FileReader;  
import java.io.IOException;  
import java.util.StringTokenizer;  


public class DBcvsdataextractor {  

    /** 
     * @param args 
    */  
    public static void main(String[] args) { 
        // TODO Auto-generated method stub  

        String fileName="D:/USPresident Wikipedia URLs Thumbs HS.csv";  

        try {    
        BufferedReader br = new BufferedReader( new FileReader(fileName));    
            StringTokenizer st = null;    
            int lineNumber = 0, tokenNumber = 0;    

            while( (fileName = br.readLine()) != null)    
            {    

                if(lineNumber++ == 0)  
                   continue;                  

                //break comma separated line using ","    
                st = new StringTokenizer(fileName, ",");    

                while(st.hasMoreTokens())    
                {    
                    //display csv values    
                    tokenNumber++;    
                    System.out.print(st.nextToken() + '\t'); 
                }    

                //new line    
                System.out.println(" ");    

                //reset token number    
                tokenNumber = 0;    

            }    
        } catch (FileNotFoundException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        } catch (IOException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        }
    }
}

Now, how do I insert that data into SQL Server?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:22:56+00:00Added an answer on May 27, 2026 at 12:22 pm

    If you really want to do this with Java, and really want to write your own CSV parser as you currently did, you can

    1. Instead of printing out each ‘CSV-file value’, you will have to store them. You could for example use an ArrayList for each column in the CSV file, and populate those while reading the CSV file
    2. Once the file is read, you can loop over those ArrayList instances again to construct one big INSERT statement for all data, or one INSERT statement for each row you encountered in the CSV file. If you would opt for the last option, it is not even necessary to use those ArrayList instances. In that case you could construct an indiviual INSERT statement while reading the CSV file, and submit it to the DB after each time a row has been read.

    I know the approach of constructing your query while reading the CSV file would be possible as well if you want to go for one big INSERT statement, but separating the INSERT from the reading of the CSV file has the big advantage you can replace your own CSV parser later on by a standard one without too much trouble.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am very new to Java, I want to add a PNG image to
I'm very new to Python (I'm coming from a JAVA background) and I'm wondering
I am really new to Java and I read that synchronized is very expensive
I am very new to Java generics and have spent an exorbitant amount of
Im very new to C programing, but have limited experience with Python, Java, and
I am very new to spring and java. I have been using mostly springsource.org
I am very new to JAVA. I have written simple program (in Linux -VIM
I am very new programmer to Java regular expressions. I do not want to
i am very new to programming in java however have a lot of experience
I'm very new to Java but I've been developing a habit to use final

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.