Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JAVA CODE FOR IMPORTING XLS FILE INTO MYSQL

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JAVA CODE FOR IMPORTING XLS FILE INTO MYSQL

    guyss this is my code for convrting xls file to mysql but got some error while running



    import java.util.*;
    import java.lang.*;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.poifs.filesystem.POIFSFileSystem;
    import org.apache.poi.ss.usermodel.Row;

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;


    public class ImportData {

    public static void main(String[] args) {
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = (Connection) DriverManager.getConnection

    ("jdbc:mysql://localhost/apnakhata_V1","root","abhi1402");
    con.setAutoCommit(false);
    PreparedStatement pstm = null ;
    FileInputStream input = new FileInputStream("D:/New

    Folder/apnakhata1.xls");
    POIFSFileSystem fs = new POIFSFileSystem( input );
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    HSSFSheet sheet = wb.getSheetAt(0);
    Row row;
    for(int i=1; i<=sheet.getLastRowNum(); i++){
    row = sheet.getRow(i);
    System.out.println(i);
    String CountryName = row.getCell(1).getStringCellValue();
    String CountryCode = row.getCell(2).getStringCellValue();
    String BankName = row.getCell(3).getStringCellValue();
    String Website = row.getCell(4).getStringCellValue();
    String Name = row.getCell(5).getStringCellValue();
    String SMS = row.getCell(6).getStringCellValue();
    int SMSNumber = (int) row.getCell(7).getNumericCellValue();
    int CustomerCare = (int) row.getCell(8).getNumericCellValue();


    String sql = "INSERT INTO bank VALUES('"+CountryName+"','"+CountryCode

    +"','"+BankName+"','"+Website+"','"+Name+"','"+SMS +"','"+SMSNumber

    +"','"+CustomerCare+"')";


    pstm = (PreparedStatement) con.prepareStatement(sql);
    pstm.execute();
    System.out.println("Import rows "+i);
    }
    con.commit();
    pstm.close();
    con.close();
    input.close();
    System.out.println("Success import excel to mysql table");
    }catch(ClassNotFoundException e){
    e.printStackTrace();
    System.out.println(e);
    }catch(SQLException ex){
    ex.printStackTrace();
    System.out.println(ex);
    }catch(IOException ioe){
    ioe.printStackTrace();
    System.out.println(ioe);
    }

    }

    }






    1
    Exception in thread "main" java.lang.IllegalStateException: Cannot get a numeric value from a text cell
    at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatc h(HSSFCell.java:647)
    at org.apache.poi.hssf.usermodel.HSSFCell.getNumericC ellValue(HSSFCell.java:672)
    at ImportData.main(ImportData.java:40)


    this is the error i got ......


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: JAVA CODE FOR IMPORTING XLS FILE INTO MYSQL

    I reckon one of these cells is not numerical:
    int SMSNumber = (int) row.getCell(7).getNumericCellValue();
    int CustomerCare = (int) row.getCell(8).getNumericCellValue();

Similar Threads

  1. JAVA CODE FOR IMPORTING XLS FILE INTO MYSQL
    By abhi1402 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: November 7th, 2013, 03:03 AM
  2. Convert DOC,XLS to PDF with Java
    By comm in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 2nd, 2013, 04:10 AM
  3. Replies: 1
    Last Post: March 15th, 2013, 10:28 AM
  4. [SOLVED] i cant make a java jar file which can search data from mysql
    By talha07 in forum JDBC & Databases
    Replies: 6
    Last Post: January 20th, 2011, 05:09 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 08:59 AM

Tags for this Thread