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 3 of 3

Thread: Import Data from Excel to Mysql Database

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Import Data from Excel to Mysql Database

    Dear Forums,


    I have a doubt i want to import data from Excel sheet to Mysql database i have done the vice versa its working fine....When i have done it by exporting data from excel to database its not accepting multiple records its taking only one record.........Can anyone help me out in this.....Any solution would be appreciable...........Thanks in advance........the below is my code.........

     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
     
    @SuppressWarnings("unused")
    public class automateImport
    {
        public static void main(String[] args) 
        {
            DBase db = new DBase();
            Connection conn = db.connect(
        "jdbc:mysql://localhost:3306/sample","root","root");
    //        db.importData(conn,args[0]);
            db.importData(conn, "/home/venugopal/data.xls");
        }
     
    }
     
    class DBase
    {
        public DBase()
        {
        }
     
        public Connection connect(String db_connect_str, 
      String db_userid, String db_password)
        {
            Connection conn;
            try 
            {
                Class.forName(  
        "com.mysql.jdbc.Driver").newInstance();
     
                conn = DriverManager.getConnection(db_connect_str, 
        db_userid, db_password);
     
            }
            catch(Exception e)
            {
                e.printStackTrace();
                conn = null;
            }
     
            return conn;    
        }
     
        public void importData(Connection conn,String filename)
        {
            Statement stmt;
            String query;
     
            try
            {
                stmt = conn.createStatement(
        ResultSet.TYPE_SCROLL_SENSITIVE,
        ResultSet.CONCUR_UPDATABLE);
     
                query = "LOAD DATA INFILE '/home/venugopal/data.xls' INTO TABLE employee (id,name,address,contactNo,email);";
     
                stmt.executeUpdate(query);
     
            }
            catch(Exception e)
            {
                e.printStackTrace();
                stmt = null;
            }
        }
    };



    Regards,

    Venu
    Last edited by StarRocks; August 29th, 2012 at 11:38 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Import Data from Excel to Mysql Database

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    My Mood
    Cheeky
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Import Data from Excel to Mysql Database

    Dear Norm,


    Done as u said me......Can you go through the code once and revert me whats wrong in the code......So that i will get an idea that where i went wrong in that......Thanks in advance.....


    Regards,

    Venu

Similar Threads

  1. Updating TableModel if changing data on DataBase(MySQL)
    By Farrukhjon in forum AWT / Java Swing
    Replies: 2
    Last Post: September 13th, 2011, 08:59 AM
  2. Replies: 6
    Last Post: September 5th, 2011, 10:02 AM
  3. [SOLVED] Passing data to a MySQL database
    By JDyson in forum JDBC & Databases
    Replies: 1
    Last Post: December 1st, 2010, 12:31 PM
  4. how to import excel file to database table using java
    By palani in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2010, 12:17 AM
  5. [SOLVED] Get Data From Database MySQL using ComboBox
    By Simple in forum Java Theory & Questions
    Replies: 2
    Last Post: June 4th, 2010, 02:45 AM