-5

I was trying to read a text file & load into SQL Server DB but when I run the following code, I am getting Array out of bound exception. Can one guide, how can I fix this issue?

package com.companyname.product;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoadTextFileintoDB
{
    public static void main(String[] args) 
    {
        DBase db = new DBase();
        Connection conn = db.connect(
    "jdbc:sqlserver://localhost\\SQLEXPRESS:1433;databaseName=ODRDev","sa","Abc-1234");
        db.importData(conn,args[0]);
    }

}

class DBase
{
    public DBase()
    {
    }

    public Connection connect(String db_connect_str, 
  String db_userid, String db_password)
    {
        Connection conn;
        try
        {
            Class.forName(  
    "com.microsoft.sqlserver.jdbc.SQLServerDriver").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 FROM 'C:/Investedge/JH765IDG_1.txt INTO TABLE InvestedgeDaily (AccountNumber,Code,Date,value);";

            stmt.executeUpdate(query);

        }
        catch(Exception e)
        {
            e.printStackTrace();
            stmt = null;
        }
    }
};

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at com.companyname.product.LoadTextFileintoDB.main(LoadTextFileintoDB.java:15)

A Sdi
  • 665
  • 2
  • 10
  • 24
R Bansal
  • 1
  • 1
  • 2
    How're you running the code? Are you passing any parameter to the main method (from command-line OR from IDE option)? – Am_I_Helpful Dec 01 '16 at 20:02
  • I am not passing any parameter. It is suppossed to read the file given in URL. – R Bansal Dec 01 '16 at 20:06
  • If you don't want to pass commandline parameters, then why do you use args[0]? – NineBerry Dec 01 '16 at 20:15
  • If I am removing args[0] from the code, it is throwing error as import data needs two parameters to pass as well so I am not sure what can be alternate to this. – R Bansal Dec 01 '16 at 20:23

1 Answers1

4

This Code triggers the exception:

args[0]

Obviously, no parameters were passed to the executable when executing it.

args is an array of the parameters passed to the executable in the command line.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • Additionally, close the connection when done working with the DB and validate the file input argument for security reasons? – Filip Dec 01 '16 at 20:05
  • How do you start the application? From the commanding or from some IDE? – NineBerry Dec 01 '16 at 20:05
  • I am using Eclipse – R Bansal Dec 01 '16 at 20:07
  • 1
    See this question http://stackoverflow.com/questions/12222153/eclipse-how-we-take-arguments-for-main-when-run – NineBerry Dec 01 '16 at 20:09
  • I added random value "test" into argument & then ran again now I am getting error : com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'FROM'. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515) – R Bansal Dec 01 '16 at 20:27
  • Well, this is a different issue. Start a new question or better yet just read the error message. It's absolutely clear – NineBerry Dec 01 '16 at 20:29
  • The "load from" command seems to be a valid syntax in Informix sql, but not in Microsoft sql – NineBerry Dec 01 '16 at 20:34