Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Storing records of a file inside database table in java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


suresh anugandula
Storing records of a file inside database table in java
1 Answer(s)      3 years and 6 months ago
Posted in : Java Beginners


Here is my requirement,

   I have a file which contains some number of records like this
  • student.csv

    1 sid sname age 2 1 suresh 24 3 2 surya 25 4 3 ashok 27

I have to display a browse button and whenever user selects student.csv and clicks on submit button, i have to store the records inside the database except headings (Here sid,sname,age are headings in student.csv file).Please help me in resolving this problem.

Thanks & Regards

Suresh


Advertisement
View Answers

June 28, 2011 at 5:20 PM


import java.io.*;
import java.sql.*;
import java.util.*;
import jxl.Cell; 
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings; 
import jxl.read.biff.BiffException;

public class ReadCSV{
ArrayList<String> list1=new ArrayList<String>();
ArrayList<String> list2=new ArrayList<String>();
ArrayList<String> list3=new ArrayList<String>();

public void getHeadingFromXlsFile(Sheet sheet){
    int columnCount = sheet.getColumns();
    for(int i = 0; i < columnCount; i++){
        System.out.println("Coloumn Count : "+sheet.getCell(i, 0).getContents());
    }
}
public void contentReading()throws Exception{
    WorkbookSettings ws = null;
    Workbook workbook = null;
    Sheet s = null;
    Cell rowData[] = null;
    int rowCount = 0;
    int columnCount = 0;
    int totalSheet = 0;


        ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        workbook = Workbook.getWorkbook(new File("C:/data.csv"), ws);

        s = workbook.getSheet(0);
        rowCount = s.getRows();
        columnCount = s.getColumns();
        for(int i = 1; i < rowCount; i++){
            rowData = s.getRow(i);
            if(rowData[0].getContents().length() != 0){ 
                for(int j = 0; j < columnCount ;j++){
                 switch(j){
                    case 0:
                        System.out.println("ID: "+rowData[j].getContents());
                        list1.add(rowData[j].getContents());
                        break;
                    case 1:
                        System.out.println("Name: "+rowData[j].getContents());
                        list2.add(rowData[j].getContents());
                        break;
                    case 2:
                        System.out.println("Age: "+rowData[j].getContents());
                        list3.add(rowData[j].getContents());
                        break;
                       }
                    }
                  }
        }
    workbook.close(); 
    try{
   Class.forName("com.mysql.jdbc.Driver");
         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root");
         Statement st=conn.createStatement();
         for(int i=0;i<list1.size();i++){
             String id=list1.get(i).toString();
             String name=list2.get(i).toString();
             String age=list3.get(i).toString();

         st.executeUpdate("insert into student(id,name,age) values('"+id+"','"+name+"','"+age+"')");
         }
        System.out.println("Inserted successfully!");
    }
    catch(Exception e){
    System.out.println(e);
      }

}
    public static void main(String[] args)throws Exception {
    try {
        ReadCSV xlReader = new ReadCSV();
        xlReader.contentReading();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
  }
}


Related Tutorials/Questions & Answers:
Storing records of a file inside database table in java
Storing records of a file inside database table in java  Here is my requirement, I have a file which contains some number of records like... the records inside the database except headings (Here sid,sname,age are headings
storing records which of a file into a table of mssql database
storing records which of a file into a table of mssql database  I have a requirement like this, A file contains some records with headers same as column names of a table. I have to store those records into a table
Advertisements
reading the records from a .xlsx file and storing those records in database table
reading the records from a .xlsx file and storing those records in database table  Here is my requirement, I want to read the records from a .xlsx file and store that records in database table. I tried like this public class
Fetch user records from a table in database
Fetch user records from a table in database  Hi. I have a field in database named stages. its datatype is varchar(60). It contains values chennai... the field table. Actually they are separated by comma. I want to take the values
storing csv into oracle database
storing csv into oracle database  i want jsp code for storing csv file into oracle database
Insert Records in Table
Insert Records in Table       The Insert data in Table is used to insert  records or rows into the table in database. The Syntax used to insert records into a table
Fetch Records from SQL database and convert into XML file
Fetch Records from SQL database and convert into XML file  Hi Experts... lets say 10 records from table / view then you need to Update the selected... file with all records into a single XML with containing as Row's. Please see
Uploading a Software and storing in the database
Uploading a Software and storing in the database  I want to upload a software(may be of maximum 20mb) through JSP, and store it in the database. The coding present in the site for uploading and storing in the database
Remote file storing in mobile
Remote file storing in mobile  Hai.. i want to do my semester project..i have selected a topic "Remote file storing in mobile"..it is in the form like any Java enabled GPRS based mobile phone users can store their images,video
Remote file storing in mobile
Remote file storing in mobile  Hai.. i want to do my semester project..i have selected a topic "Remote file storing in mobile"..it is in the form like any Java enabled GPRS based mobile phone users can store their images,video
storing xml into database - XML
storing xml into database   hi i have an xml file it contains... .......with it i know how to persist a simple xml file into data base but i m finding some difficulty to start with i m sending u the xml file
How to download web page table data, export the table records in an excel file and save
How to download web page table data, export the table records in an excel file and save  i have a web page(.jsp) which contains the table of 4 to 5 columns. i m displaying the table using in my jsp page. below this table i want
Reading Text file and storing in map
Reading Text file and storing in map  Hi I have multiple text files. I want to read thoses files and store those records in map. Map will be of "LinkedHashMap<String, Map<String, String>>" type. Please let me know
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
Backup selected records into txt file
to copies the selected records or rows from a table into text file. Further... table. This text file is used to recover the records in the database  ... Backup selected records into txt file   
PHP WHERE clause example to fetch records from Database Table
the data within the MYSQL database table. You can also fetch the results using... Server and will fetch the results from the database table employee date is equal... existing database table. You can create your own
php csv file uploding into mysql database table.
php csv file uploding into mysql database table.  hai friends, i... fields. i have one mysql database table with two files fields, when ever i am uploding csv file, i want to fetch the datas in corresponding fields in table. can
searching inside a file for details
searching inside a file for details  i created a file with name,country,state etc...when i'm searching for the name, i have to get other details like country,state etc...please do help
Write Text File to Table
the records of a simple text file and write (insert) into a simple table in MySQL database. All records are written in the simple text file and again, if you need... to be inserted the records into a database table. This program uses the simple
Tag Inside Table in HTML
Tag Inside Table in HTML       The Tutorial illustrates an example from Tag Inside Table in HTML.In this code we define a HTML page and show you to create a tag inside a Table
Arrange a Column of Database Table
that arrange the records of database table in descending order. The descending... the records of database table in descending order. The SELECT specifies...; DESC: This query helps us  to arrange the records of database table
to fetch data from ms word and storing into database
to fetch data from ms word and storing into database  i want to know how to fetch datafields from ms word and storing into database??? please answer soon .its urgent
getting and storing dropdown list in database in jsp
getting and storing dropdown list in database in jsp  i have a drop... database. but unable to store the selected value in database table. please help... lyk this. but it storing null value in the database. :\ what to do
Write records into text file from database
Write records into text file from database You have already learnt how to insert records from text file to database. But here we are going to retrieve records from database and store the data into the text file. For this purpose, we have
Delete a Records in Database
to delete Records in Database. In this Tutorial, we create a table country, in which we... Delete a Records in Database   ... from the table. The generalized Syntax  used for delete query in database
JDBC : Duplicate records in a table
JDBC : Duplicate records in a table In this tutorial, you will learn how to find the duplicate records in a table. Duplicate records in a table : You can check for duplicate records of table. You can group record by any of fields
Storing content from file path to an array
Storing content from file path to an array  Hi, I have a path... would like to access the "Animation.txt" file from this path and store the contents in this text file to an array. I am using jsp. I can access my path but how
how to display records from database
how to display records from database  I want to display records from database in tables, the database is having 2000 records and i want to display 20 records at a time and to use next and previous link buttons to show
complex xml parsing and storing in database - XML
complex xml parsing and storing in database  Hi Experts ,i want to parse my xml document and store it in mysql database. This is my code. How to parse this complex data. EDI_DC40 800 0000000000557748
Export Database table to CSV File
Export Database table to CSV File In this tutorial, you will learn how... as writing into a CSV file. Here is a database table is to be extracted: Example... created a database connection and fetch the records from the table using
Update Records in Database
Update Records in Database       The Update Records in database is used to modify... to retrieve the records from a table country. The Table show you the record from
Advertisements
 

 

 

DMCA.com