How to read the CSV Files into Database Table

Hi
Friends i have a Table called tblstudent this has the following fields Student ID, StudentName ,Class,Father_Name, Mother_Name.
Now i have a CSV File with 1500 records with all These Fields. Now in my Program there is a need for me to read all these Records into this Table tblstudent.
Note: I have got already 2000 records in the Table tblstudent now i would like to read all these CSV File records into the Table tblstudent.
Please give me some examples to do this
Thank your for your service
Cheers
Jofin

1) Read the CSV file line by line using BufferedReader.
2) Convert each line (record) to a List and add it to a parent List. If you know the columns before, you might use a List of DTO's.
3) Finally save the two-dimensional List or the List of DTO's into the datatable using plain JDBC or any kind of ORM (Hibernate and so on).
This article contains some useful code snippets to parse a CSV: http://balusc.xs4all.nl/srv/dev-jep-csv.html

Similar Messages

  • How to read/write .CSV file into CLOB column in a table of Oracle 10g

    I have a requirement which is nothing but a table has two column
    create table emp_data (empid number, report clob)
    Here REPORT column is CLOB data type which used to load the data from the .csv file.
    The requirement here is
    1) How to load data from .CSV file into CLOB column along with empid using DBMS_lob utility
    2) How to read report columns which should return all the columns present in the .CSV file (dynamically because every csv file may have different number of columns) along with the primariy key empid).
    eg: empid report_field1 report_field2
    1 x y
    Any help would be appreciated.

    If I understand you right, you want each row in your table to contain an emp_id and the complete text of a multi-record .csv file.
    It's not clear how you relate emp_id to the appropriate file to be read. Is the emp_id stored in the csv file?
    To read the file, you can use functions from [UTL_FILE|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm#BABGGEDF] (as long as the file is in a directory accessible to the Oracle server):
    declare
        lt_report_clob CLOB;
        l_max_line_length integer := 1024;   -- set as high as the longest line in your file
        l_infile UTL_FILE.file_type;
        l_buffer varchar2(1024);
        l_emp_id report_table.emp_id%type := 123; -- not clear where emp_id comes from
        l_filename varchar2(200) := 'my_file_name.csv';   -- get this from somewhere
    begin
       -- open the file; we assume an Oracle directory has already been created
        l_infile := utl_file.fopen('CSV_DIRECTORY', l_filename, 'r', l_max_line_length);
        -- initialise the empty clob
        dbms_lob.createtemporary(lt_report_clob, TRUE, DBMS_LOB.session);
        loop
          begin
             utl_file.get_line(l_infile, l_buffer);
             dbms_lob.append(lt_report_clob, l_buffer);
          exception
             when no_data_found then
                 exit;
          end;
        end loop;
        insert into report_table (emp_id, report)
        values (l_emp_id, lt_report_clob);
        -- free the temporary lob
        dbms_lob.freetemporary(lt_report_clob);
       -- close the file
       UTL_FILE.fclose(l_infile);
    end;This simple line-by-line approach is easy to understand, and gives you an opportunity (if you want) to take each line in the file and transform it (for example, you could transform it into a nested table, or into XML). However it can be rather slow if there are many records in the csv file - the lob_append operation is not particularly efficient. I was able to improve the efficiency by caching the lines in a VARCHAR2 up to a maximum cache size, and only then appending to the LOB - see [three posts on my blog|http://preferisco.blogspot.com/search/label/lob].
    There is at least one other possibility:
    - you could use [DBMS_LOB.loadclobfromfile|http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_lob.htm#i998978]. I've not tried this before myself, but I think the procedure is described [here in the 9i docs|http://download.oracle.com/docs/cd/B10501_01/appdev.920/a96591/adl12bfl.htm#879711]. This is likely to be faster than UTL_FILE (because it is all happening in the underlying DBMS_LOB package, possibly in a native way).
    That's all for now. I haven't yet answered your question on how to report data back out of the CLOB. I would like to know how you associate employees with files; what happens if there is > 1 file per employee, etc.
    HTH
    Regards Nigel
    Edited by: nthomas on Mar 2, 2009 11:22 AM - don't forget to fclose the file...

  • How to read a CSV file into the portal

    hi all,
    I want to read the content of CSV file that is avaliable in my desktop.
    plz help me with suitable code
    Regards
    Savitha
    Edited by: Savitha S R on Jun 1, 2009 8:25 AM

    Please use this code for that
    REPORT  znkp_upload_csv line-size 400.
    DATA : v_filename TYPE string.
    PARAMETER : p_file LIKE rlgrap-filename DEFAULT 'C:\Documents and Settings\Administrator\Desktop\JV.csv' .
    *Types for Reading CSV file
    TYPES : BEGIN OF x_csv,
              text(400) TYPE c,
            END OF x_csv.
    *Global internal table for reading CSV file
    DATA : lt_csv TYPE TABLE OF x_csv.
    *Global work area for reading CSV file
    DATA : wa_csv LIKE LINE OF lt_csv.
    v_filename = p_file.
    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename                      = v_filename
      FILETYPE                      = 'ASC'
      HAS_FIELD_SEPARATOR           = ' '
      HEADER_LENGTH                 = 0
      READ_BY_LINE                  = 'X'
      DAT_MODE                      = ' '
      CODEPAGE                      = ' '
      IGNORE_CERR                   = ABAP_TRUE
      REPLACEMENT                   = '#'
      CHECK_BOM                     = ' '
      VIRUS_SCAN_PROFILE            =
      NO_AUTH_CHECK                 = ' '
    IMPORTING
      FILELENGTH                    =
      HEADER                        =
      TABLES
        data_tab                      = lt_csv
    EXCEPTIONS
       file_open_error               = 1
       file_read_error               = 2
       no_batch                      = 3
       gui_refuse_filetransfer       = 4
       invalid_type                  = 5
       no_authority                  = 6
       unknown_error                 = 7
       bad_data_format               = 8
       header_not_allowed            = 9
       separator_not_allowed         = 10
       header_too_long               = 11
       unknown_dp_error              = 12
       access_denied                 = 13
       dp_out_of_memory              = 14
       disk_full                     = 15
       dp_timeout                    = 16
       OTHERS                        = 17
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
    DATA : BEGIN OF item OCCURS 0 ,
            t1(20) TYPE c,
            t2(20) TYPE c,
            t3(20) TYPE c,
            t4(20) TYPE c,
            t5(20) TYPE c,
            t6(20) TYPE c,
            t7(20) TYPE c,
            t8(20) TYPE c,
           END OF item.
    DATA : txt(1) TYPE c. " 1-Header 2-Item
    LOOP AT lt_csv into wa_csv.
    split wa_csv-text at ',' into item-t1
                                  item-t2
                                  item-t3
                                  item-t4
                                  item-t5
                                  item-t6
                                  item-t7
                                  item-t8.
    append item.
    clear item.
    ENDLOOP.
    Check ITEM TABLE
    Regards
    Naresh

  • Question about reading csv file into internal table

    Some one (thanks those nice guys!) in this forum have suggested me to use FM KCD_CSV_FILE_TO_INTERN_CONVERT to read csv file into internal table. However, it can be used to read a local file only.
    I would like to ask how can I read a CSV file into internal table from files in application server?
    I can't simply use SPLIT as there may be comma in the content. e.g.
    "abc","aaa,ab",10,"bbc"
    My expected output:
    abc
    aaa,ab
    10
    bbb
    Thanks again for your help.

    Hi Gundam,
    Try this code. I have made a custom parser to read the details in the record and split them accordingly. I have also tested them with your provided test cases and it work fine.
    OPEN DATASET dsn FOR input IN TEXT MODE ENCODING DEFAULT.
    DO.
    READ DATASET dsn INTO record.
      PERFORM parser USING record.
    ENDDO.
    *DATA str(32) VALUE '"abc",10,"aaa,ab","bbc"'.
    *DATA str(32) VALUE '"abc","aaa,ab",10,"bbc"'.
    *DATA str(32) VALUE '"a,bc","aaaab",10,"bbc"'.
    *DATA str(32) VALUE '"abc","aaa,ab",10,"b,bc"'.
    *DATA str(32) VALUE '"abc","aaaab",10,"bbc"'.
    FORM parser USING str.
    DATA field(12).
    DATA field1(12).
    DATA field2(12).
    DATA field3(12).
    DATA field4(12).
    DATA cnt TYPE i.
    DATA len TYPE i.
    DATA temp TYPE i.
    DATA start TYPE i.
    DATA quote TYPE i.
    DATA rec_cnt TYPE i.
    len = strlen( str ).
    cnt = 0.
    temp = 0.
    rec_cnt = 0.
    DO.
    *  Start at the beginning
      IF start EQ 0.
        "string just ENDED start new one.
        start = 1.
        quote = 0.
        CLEAR field.
      ENDIF.
      IF str+cnt(1) EQ '"'.  "Check for qoutes
        "CHECK IF quotes is already set
        IF quote = 1.
          "Already quotes set
          "Start new field
          start = 0.
          quote = 0.
          CONCATENATE field '"' INTO field.
          IF field IS NOT INITIAL.
            rec_cnt = rec_cnt + 1.
            CONDENSE field.
            IF rec_cnt EQ 1.
              field1 = field.
            ELSEIF rec_cnt EQ 2.
              field2 = field.
            ELSEIF rec_cnt EQ 3.
              field3 = field.
            ELSEIF rec_cnt EQ 4.
              field4 = field.
            ENDIF.
          ENDIF.
    *      WRITE field.
        ELSE.
          "This is the start of quotes
          quote = 1.
        ENDIF.
      ENDIF.
      IF str+cnt(1) EQ ','. "Check end of field
        IF quote EQ 0. "This is not inside quote end of field
          start = 0.
          quote = 0.
          CONDENSE field.
    *      WRITE field.
          IF field IS NOT INITIAL.
            rec_cnt = rec_cnt + 1.
            IF rec_cnt EQ 1.
              field1 = field.
            ELSEIF rec_cnt EQ 2.
              field2 = field.
            ELSEIF rec_cnt EQ 3.
              field3 = field.
            ELSEIF rec_cnt EQ 4.
              field4 = field.
            ENDIF.
          ENDIF.
        ENDIF.
      ENDIF.
      CONCATENATE field str+cnt(1) INTO field.
      cnt = cnt + 1.
      IF cnt GE len.
        EXIT.
      ENDIF.
    ENDDO.
    WRITE: field1, field2, field3, field4.
    ENDFORM.
    Regards,
    Wenceslaus.

  • How to import an .csv file into the database?

    and can we code the program in JSP to import the.csv file into the database.

    It is better to use Java class to read the CSV file and store the contents in the database.
    You can use JSP to upload the CSV file to the server if you want, but don't use it to perform database operations.
    JSPs are good for displaying information on the front-end, and for displaying HTML forms, there are other technologies more suitable for the middle layer, back end and the database layer.
    So break you application into
    1) Front end - JSPs to display input html forms and to display data retrieved from the database.
    2) Middle layer - Servlets and JavaBeans to interact with JSPs. The code that reads the CSV file to parse it's contents should be a Java Class in the middle layer. It makes use of Java File I/O
    3) Database layer - Connects to the database using JDBC (Java Database Connectivity), and then writes to the database with SQL insert statements.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Keeping the above concepts in mind, first build a simple JSP and get it to work,
    then research on Google , for Java File I/O , discover how to read a file,
    Then search on how to readh a CSV file using Java.
    After researching you should be able to read the CSV file line by line and store each line inside a Collection.
    Then research on Google, on how to write to the database using JDBC
    Write a simple program that inserts something to a dummy table in the database.
    Then, read the data stored in the Collection, and write insert statements for each records in the collection.

  • VISA: How to get the CSV file through C/C++ code?

    How to get the CSV file through C/C++ code?
    I'm communicating with a USB oscilloscope using VISA.

    Thank you for your reply.
    I am using Apache Commons File Upload. Here is the code where I got the "fi".
         DiskFileUpload fu = new DiskFileUpload();
              fu.setSizeMax(1000000000);
              List fileItems = fu.parseRequest(request);
             Iterator itr = fileItems.iterator();
              FileItem fi = (FileItem)itr.next();
              if(!fi.isFormField())
                File tempFileRef  = new File(fi.getName());
         System.out.println("Field = "+tempFileRef.getAbsolutePath());
         }

  • How to embed the html file into form 6i?

    How to embed the html file into form. What control activeX should i use? please help!!!!!!

    You can use web.show_document function for this purpose. See
    form builder help topics for more details.
    Hope this helps.

  • How to read the properties file available in Server File structure in webdy

    hi all,
    I have developed one webdynpro application. In this application i need to access mdm server to continue. For getting the connection i need to pass the IP addresses.
    Can i have code  how to read the properties file which is residing in the server file. with out included along with the application. keeping some where in the file structure in the server. I want to read that properties file by  maintain the iP addresses and users in  properties file based on the key i want to read like below.
    servername="abcServer"
    username="john"
    password="test123"
    Please send me the code how to read this properties file from the file structure and how to read this values by key  in webdynpro application
    Regards
    Vijay

    Hi Vijay,
    You can try this piece of code too:
    Properties props = new Properties();
    //try retrieve data from file
    //catch exception in case properties file does not exist
    try {
             props.load(new FileInputStream("c:\property\Test.properties")); //File location
             String serverName = props.getProperty("servername"); //Similarly, you can access the other properties
             if(serverName==null)
               //....do appropriate handling
         }     catch(IOException e)
                   e.printStackTrace();
    Regards,
    Alka.

  • How to break the PDF file into images?

    How to break the PDF file into images? There should be settings in Photoshop CS 6 that imports PDF file and break it into images.  I have a short instruction:
    2.    Open the file in Photoshop.
    3.    A new window should open to Import PDF.  Click on images, not pages  import pdf into Photoshop.doc
    4.    Select all the files shown (shift + click), Click O.K.  Four files should open on your screen.
      Was anybody successful with that?

    Whether this is available solely depends on the structure of the PDF and whether it actually contains whole images that can be extracted separately. Depending on whatgoing on in the PDF this may simply not be the case and the images have been tiled and split up in multiple "objects" whose appearance can only be retained by rasrerizing the whole page...
    Mylenium

  • How to transfer log files into Database Table.

    Hello,
    I have a requirement. My Front end is Oracle Application. If any user deletes the data from front end screen. One log file should be generated. That file will save in one folder in my server. And That log file consists which code is deleted.
    And the user name who deleted the code, time and deleting status..
    Now my requirement is i have to develop a report to display the log file data. But i dont have a database table to retrieve the data. The data consists Log files.
    How to transfer the Log files in DB table.
    I need a data in DB table to develop a report.
    Thanks...

    How do I ask a question on the forums?
    SQL and PL/SQL FAQ
    is application 3-tier

  • How to read the backup file of my own iphone...

    how to read the backup file of my own iphone... becase I'm running out of space in my iphone so  need to delete some photos. I wanna know do i have to copy photos to my computer or is it available in the back up file ????

    Always move (copy and delete) your photos from Camera Roll to your computer.
    Camera Roll is the digital camera card.

  • Upload .txt file into Database Table

    Hi,
    I was wondering if someone could please point me in the right direction. I've been looking around the forum but can't find anything to help me achieve the following.
    I would like to be able to upload a .txt file using a webpage. Then store the information inside this file into database tables.
    eg. contents of mytextfile.txt:
    richard
    10 anywhere street, anytown, somewhere
    111 222 333 444
    joe
    9 somestreet, elsewhere
    999 888 777 666
    peter
    214 nearby lane, overhere
    555 555 555 555
    I would like to insert this data into a table.
    eg. table name = CONTACTS
    userid = primary key (using sequence)
    username = (line 1 - richard, joe, peter)
    address = (line 2)
    phone_no = (line 3)
    As you can see the records will appear 1 at a time and will have a blank line between records. Is there anyway for me to upload a file like this and have it placed into tables?
    I have seen http://otn.oracle.com/products/database/htmldb/howtos/howto_file_upload.html but this seems to be for uploading a whole file and downloading the same file, rather than extracting data from the file.
    I hope I have managed to explain my problem.
    Many thanks,
    Richard.

    Richard,
    HTML DB allows you to upload CSV files via the Data Workshop. That data would then be parsed and inserted into a specific table. Alternatively, if you have your data in an Excel spreadsheet, and it is less than 32k, you can copy & paste the data directly into HTML DB's Data Workshop, which will then parse and import it into the Oracle database.
    The one obstacle you may have to overcome is converting your data from the format you outlined to CSV format. Specifically, you would have to make this:
    richard
    10 anywhere street, anytown, somewhere
    111 222 333 444
    Look something like this:
    "richard","10 anywhere street, anytown, somewhere","111 222 333 444"
    Hope this helps,
    - Scott -

  • Uploading CSV file into internal table

    Hi,
    I want to upload a CSV file into internal table.The flat file is having values as below:
    'AAAAA','2003-10-11 07:52:37','167','Argentina',NULL,NULL,NULL,NULL,NULL,'MX1',NULL,NULL,'AAAA BBBB',NULL,NULL,NULL,'1',NULL,NULL,'AR ',NULL,NULL,NULL,'ARGENT','M1V','MX1',NULL,NULL,'F','F','F','F','F',NULL,'1',NULL,'MX','MMI ',NULL
    'jklhg','2004-06-25 08:01:57','456','hjllajsdk','MANAGUA   ',NULL,NULL,'265-5139','266-5136 al 38','MX1',NULL,NULL,'hjgkid GRÖBER','sdfsdf dfs asdfsdf 380 ad ased,','200 as ads, sfd sfd abajao y 50 m al sdf',NULL,'1',NULL,NULL,'NI ',NULL,NULL,NULL,'sdfdfg','M1V','dds',NULL,NULL,
    Here I can not even split at ',' because some of the values are having value like NULL and some have values with comma too,
    The delimiter is a quote and the separator is a comma here.
    Can anyone help on this?
    Thanks.
    Edited by: Ginger on Jun 29, 2009 9:08 AM

    As long as there can be a comma in a text literal you are right that the spilt command doesn't help. However there is one possibility how to attack this under one assumption:
    - A comma outside a text delimiter is always considered a separator
    - A comma inside a text delimiter is always considered a comma as part of the text
    You have to read you file line by line and then travel along the line string character by character and setting a flag or counter for the text delimiters:
    e.g.
    "Text","Text1, Text2",NULL,NULL,"Text"
    String Index  1: EQ " => lv_delimiter = 'X'
    String Index  2: EQ T => text literal (because lv_delimiter = 'X')
    String Index  3: EQ e => text literal (because lv_delimiter = 'X')
    String Index  4: EQ x => text literal (because lv_delimiter = 'X')
    String Index  5: EQ t => text literal (because lv_delimiter = 'X')
    String Index  6: EQ " => lv_delimiter = ' ' (because it was 'X' before)
    String Index  7: EQ , => This is a separator because lv_delimiter = ' '
    String Index  8: EQ " => lv_delimiter = 'X' (because it was ' ' before)
    String Index  9: EQ T => text literal (because lv_delimiter = 'X')
    String Index 10: EQ e => text literal (because lv_delimiter = 'X')
    String Index 11: EQ x => text literal (because lv_delimiter = 'X')
    String Index 12: EQ t => text literal (because lv_delimiter = 'X')
    String Index 13: EQ 1 => text literal (because lv_delimiter = 'X')
    String Index 14: EQ , => text literal (because lv_delimiter = 'X')
    String Index 15: EQ T => text literal (because lv_delimiter = 'X')
    Whenever you hit a 'real' separator (lv_delimiter = ' ') you pass the value of the string before that up to the previous separator into the next structure field.
    This is not an easy way to do it, but if you might have commas in your text literal and NULL values I gues it is probably the only way to go.
    Hope that helps,
    Michael

  • Getting Issue while uploading CSV file into internal table

    Hi,
    CSV file Data format as below
         a             b               c              d           e               f
    2.01E14     29-Sep-08     13:44:19     2.01E14     SELL     T+1
    actual values of column   A is 201000000000000
                     and  columen D is 201000000035690
    I am uploading above said CSV file into internal table using
    the below coding:
    TYPES: BEGIN OF TY_INTERN.
            INCLUDE STRUCTURE  KCDE_CELLS.
    TYPES: END OF TY_INTERN.
    CALL FUNCTION 'KCD_CSV_FILE_TO_INTERN_CONVERT'
        EXPORTING
          I_FILENAME      = P_FILE
          I_SEPARATOR     = ','
        TABLES
          E_INTERN        = T_INTERN
        EXCEPTIONS
          UPLOAD_CSV      = 1
          UPLOAD_FILETYPE = 2
          OTHERS          = 3.
      IF SY-SUBRC <> 0.
        MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.
    am getting all columns data into internal table,
    getting problem is columan A & D. am getting values into internal table for both; 2.01E+14. How to get actual values without modifying the csv file format.
    waiting for your reply...
    thanks & regards,
    abhi

    Hi Saurabh,
    Thanks for your reply.
    even i can't double click on those columns.
    b'se the program needs be executed in background there can lot of csv file in one folder. No manual interaction on those csv files.
    regards,
    abhi

  • How to store the zip file in oracle table?

    hi,
    How to store the zip file in oracle table ?
    is it possible to unzip and read the file ?
    Thanks
    Rangan S

    SQL> DESC BLOB_TABLE;
    Name Type Nullable Default Comments
    A INTEGER Y
    B BLOB Y
    SQL> INSERT INTO BLOB_TABLE VALUES(5,BLOB('MWDIR_TST','TEST.ZIP'));
    INSERT INTO BLOB_TABLE VALUES(5,BLOB('MWDIR_TST','TEST.ZIP'))
    ORA-00904: "BLOB": invalid identifier
    SQL> INSERT INTO BLOB_TABLE VALUES(5,('MWDIR_TST','TEST.ZIP'));
    INSERT INTO BLOB_TABLE VALUES(5,('MWDIR_TST','TEST.ZIP'))
    ORA-00907: missing right parenthesis
    SQL> INSERT INTO BLOB_TABLE VALUES(5,('\\MWDIR_TST\TEST.ZIP'));
    INSERT INTO BLOB_TABLE VALUES(5,('\\MWDIR_TST\TEST.ZIP'))
    ORA-01465: invalid hex number
    SQL> INSERT INTO BLOB_TABLE VALUES(5,('\\MWDIR_TST\TEST.ZIP'));

Maybe you are looking for

  • Got the following reply from db-kernel: SQL-Code :-903

    Dear Experts, I am having a problem running MaxDB Data backup on Netbackup.... Please se log below and suggest. 2011-04-11 13:30:38 Using environment variable 'TEMP' with value 'C:\Windows\TEMP' as directory for temporary files and pipes. Using conne

  • Can i buy a MacBook/Pro from the US online store from another country?

    Hi guys, is it possible to pay for the US online store for buying a MacBook / Pro and ship it within the states of course?? one of my family members are there and they are not that much into computers, is it possible to buy it from here (Jordan) usin

  • How to configure the outline color of a bar in a graph

    I have a user who has a graph with many datapoints. He wants these to be bars, but with so many datapoints on the graph, the bar outlines are more prevalent than the set bar colors.  I am unable to locate where the outline color of a bar in a graph i

  • Wireless controller 5500 and aironet 1142

    We have a wireless controller 5500 at a hub site.  The remote sites are running the aironet 1142 wap in controller mode.  We configure dhcp option 43 at the remote isr's and the 1142 remote site waps's join the controller's as they should.  The 1142'

  • WDS and Unattended queries/clarification (Server 2012)

    Hi Folks I am studying for my 70-411 exam and have a few points that I would like clarification on relating to unattended installations and WDS: 1: When you create an unattend.xml for the 'Windows Deployment Services client',  in SIM which image do y