-1

In this program I tried to get the input from the console to chose whether to view or insert into the database. When i try to insert the details in the database As soon as i enter Employee ID, It automatically skips Name and it asks for entering the department. Please Explain me?

package jdbc.org;
import java.sql.*;
import java.util.*;

public class EmpDetails 
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/Employee";


static final String USER = "root";
static final String PASS = "";

public static void main(String[] args) 
{
    int EmpID;
    String Name, Dept, Email;
    Connection con = null;
    Statement s=null;  
    ResultSet rs=null;  
    int ch;  
    boolean flag=true;  
    Scanner sc=new Scanner(System.in);  
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Successfully registered driver"); 

        System.out.println("Connecting to a selected database...");
        con = DriverManager.getConnection(DB_URL, USER, PASS);
        System.out.println("Connected database successfully...");
    }
    catch(Exception e)  
    {  
    System.out.println("Error1" +e);  

    }    
    do  
    {   
    System.out.println("Press 1 to INSERT into the DB");  
    //System.out.println("Press 2 to DELETE from DB");  
    //System.out.println("Press 3 to UPDATE into DB");  
    System.out.println("Press 2 to VIEW ALL from DB");  
    System.out.println("Press 3 to EXIT");  
    System.out.println("Enter Your Choice");  
    ch=Integer.parseInt(sc.nextLine());

    switch(ch)
    {
    case 1:
        System.out.println("Enter Your Employee ID:\n");
        EmpID=sc.nextInt();

        System.out.println("Enter Your Name:\n");
        Name=sc.nextLine();

        System.out.println("Enter Your Department:\n");
        Dept=sc.nextLine();

        System.out.println("Enter Your Email:\n");
        Email=sc.nextLine();

        try
        {
            String query="Insert into Employee values('"+EmpID+"','"+Name+"','"+Dept+"','"+Email+"')";
            s=con.createStatement();  
            s.executeQuery(query);  
            System.out.println("Row Inserted"); 

        }
        catch(Exception e)
        {

            System.out.println("Error2" +e);  

           break; 
        }

         case 2:  
             try  
               {  

               String query="select * from Employee ";  
               s=con.createStatement();  
               rs=s.executeQuery(query);  
               boolean rec=rs.next();  
               while(!rec)  

               {  
               System.out.println("No Record");  
               }      

               do  
               {  
                  EmpID=rs.getInt(1);  
                  Name=rs.getString(2);  
                  Dept=rs.getString(3);
                  Email=rs.getString(4);

                  System.out.print(EmpID+"\t");  
                  System.out.print(Name+"\t");  
                  System.out.println(Dept+"\t");  
                  System.out.println(Email);
               }while(rs.next());  

             s.close();  
             con.close();  

               }  
             catch(Exception e)  
                {  
                System.out.println("Error2" +e);  

                }      
              break;  
         case 3:  
              System.exit(1);  
                break;  

             default:  
               System.out.println("Default Case");  


    }
    System.out.println("Do You want to continue:Yes/No)");  
    String str=sc.nextLine();  
    if(str.equals("Yes")|| str.equals("Y"))  
    flag=true;  
    if(str.equals("No")||str.equals("N"))  
    flag=false;  

    }while(flag);    


}

}

  • 2
    possible duplicate of [Skipping nextLine() after using next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) – Madhawa Priyashantha Sep 11 '15 at 10:38
  • Also I wouldn't call that a [minimal example](http://stackoverflow.com/help/mcve). – fabian Sep 11 '15 at 10:42
  • Also why are you parsing `ch ` to an `int ` : `ch=Integer.parseInt(sc.nextLine()); `. Why don't you use `nextInt(); ` as you did in your switch-block? – Tom Wellbrock Sep 11 '15 at 11:12

1 Answers1

1

This is because sc.nextInt() does not consume new line. So either

  String tmp = sc.nextLine();
  try { empid = Integer.parseInt(tmp); }
  catch (Exception e) { ... }

or

  System.out.println("Enter Your Employee ID:\n");
  EmpID=sc.nextInt();
  sc.nextLine();  /* consume new line here */

  System.out.println("Enter Your Name:\n");
  Name=sc.nextLine();

should work.