用户输入插入

时间:2018-07-10 18:10:09

标签: java jdbc

我想在数据库中插入100条记录。这是我的代码,但是我得到了inputmismatchexception。有人可以告诉我我的代码有什么问题吗?我是Java JDBC的新手。

    int count = 0;
    while (count < 100) {
    String emp = sc.nextLine();
    int sal = sc.nextInt();
    int empid = sc.nextInt();

    try {

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/practice","root","admin");
        Statement st = con.createStatement();
        int rs = st.executeUpdate("insert into practicetable values ('"+emp+"','"+sal+"','"+empid+"')");
        System.out.println("success");
        count++;

}catch(Exception e) {
    e.printStackTrace();
}
    }

} }

1 个答案:

答案 0 :(得分:0)

读取整数时,您需要读取(并省略)换行符:

String emp = sc.nextLine();
int sal = sc.nextInt();
//read and omit the break line
sc.nextLine();
int empid = sc.nextInt();