无法在Java中正确输入Input

时间:2015-09-11 10:36:52

标签: java jdbc

在这个程序中,我试图从控制台获取输入,以选择是否查看或插入数据库。当我尝试在数据库中插入详细信息时,只要我输入员工ID,它就会自动跳过名称,并要求输入部门。请解释一下?

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);    


}

}

1 个答案:

答案 0 :(得分:1)

这是因为sc.nextInt()不消耗新行。所以要么

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

  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();

应该有用。