2

I am trying to fetch some records from a txt file and put those in Database in the following Java Program

package Java_Demo;
import java.sql.*;
import java.util.*;
import java.io.*;

public class Jdbc_Demo {


    public static void main(String ...args)throws ClassNotFoundException,SQLException,FileNotFoundException {

        FileInputStream fin=new FileInputStream("C:/Users/steve-pc/Desktop/Employees.txt");
        Scanner s=new Scanner(fin);
        s.useDelimiter(",|\\n");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.22.22.131:1521:orcl","Demo","demo");
        while(s.hasNext())
        {

        PreparedStatement st=con.prepareStatement("Insert into Employee values(?,?,?)");

        String name=s.next();
        int  id=s.nextInt();
        int sal=s.nextInt();

        st.setString(1,name);
        st.setInt(2,id);
        st.setInt(3,sal);

        int result=st.executeUpdate();
        System.out.println("Records Changed: "+ result);

        }
        con.close();

    }

 }

The txt file from which I am fetching the data has following format

Steve,12349,550000

Mark,54321,250000

Bill,65478,350000

Additionally the EMPLOYEE table has following format

Name Id Salary

Varachar2 Number Number

But the code is generating following ERROR

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:909)

at java.util.Scanner.next(Scanner.java:1530)

at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Java_Demo.Jdbc_Demo.main(Jdbc_Demo.java:24)
Steve
  • 747
  • 1
  • 11
  • 21
  • int id=s.nextInt(); & int sal=s.nextInt(); is taking in integer while your scanner will read your file as String instead. Convert it into integer. – Sky May 02 '14 at 10:15
  • @Sky I don't think so.. if I am keeping only single row in my txt file, then it is workin perfectly – Steve May 02 '14 at 10:37

3 Answers3

2

Make the change as below:

public class ScannerTest {


    public static void main(String ...args)throws ClassNotFoundException,SQLException,FileNotFoundException {

        File f =new File("/home/rahul/Desktop/emp.txt");
        Scanner s=new Scanner(f);
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.22.22.131:1521:orcl","Demo","demo");
        while(s.hasNext())
        {

        PreparedStatement st=con.prepareStatement("Insert into Employee values(?,?,?)");

        String rcd = s.next();
        Scanner s1 = new Scanner(rcd);
        s1.useDelimiter(",|\\n");

        String name=s1.next();
        int  id=s1.nextInt();
        int sal=s1.nextInt();

        st.setString(1,name);
        st.setInt(2,id);
        st.setInt(3,sal);

        int result=st.executeUpdate();
        System.out.println("Records Changed: "+ result);

        }
        con.close();

    }

 }
}
Rahul
  • 3,401
  • 3
  • 13
  • 27
  • I don't get the logic of using "rcd" variable – Steve May 02 '14 at 10:33
  • IN above logic, by using first scanner you are reading a line as a string and this string is being passed to another scanner which is splitting the string on the given pattern and you are calling nextInt(). – Rahul May 02 '14 at 12:13
2

s.nextInt(); is giving error for you. Because the scanner next cannot be converted into integer. I would suggest, you use s.next for all three and then Integer.parseInt(String) method to convert them into int

Hirak
  • 3,461
  • 1
  • 19
  • 32
  • I don't think so.. if I am keeping only single row in my txt file, then it is workin perfectly – Steve May 02 '14 at 10:27
1

I wonder if you have whitespace in your file?

If so, you can use something like this to ignore it

            scanner.useDelimiter("\\s*,\\s*|\\n");

Or read each token as a string and then tidy and convert to int.

TedTrippin
  • 3,325
  • 4
  • 23
  • 41