问题描述
importjava.sql.*;importjava.util.*;publicclassEmployeeManager{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersc=newScanner(System.in);while(true){System.out.println("1添加员工");System.out.println("2删除员工");System.out.println("3查询员工");System.out.println("4更新员工信息");System.out.println("0退出");try{Class.forName("com.mysql.jdbc.Driver");Connectioncon=DriverManager.getConnection("jdbc:mysql://localhost:3306/yuangong","root","mozhe");Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);System.out.println("请输入选项");intnum=sc.nextInt();if(num==0){System.out.println("谢谢使用");stmt.close();con.close();break;}elseif(num==1){System.out.println("请输入员工id");intid=sc.nextInt();System.out.println("请输入姓名");Stringname=sc.next();System.out.println("请输入年龄");intage=sc.nextInt();System.out.println("请输入工资");floatsalary=sc.nextFloat();ResultSetrs=stmt.executeQuery("select*fromemployeeinfo");rs.moveToInsertRow();rs.updateInt("id",id);rs.updateString("name",name);rs.updateInt("age",age);rs.updateFloat("salary",salary);rs.insertRow();rs.close();stmt.close();con.close();}elseif(num==2){System.out.println("请输入要删除的员工id");intid=sc.nextInt();ResultSetrs=stmt.executeQuery("select*fromemployeeinfowhereid=id");rs.last();rs.deleteRow();rs.close();stmt.close();con.close();}elseif(num==3){ResultSetrs=stmt.executeQuery("select*fromemployeeinfo");System.out.println("id号"+"t"+"姓名"+"t"+"年龄"+"t"+"工资");while(rs.next()){System.out.print(rs.getInt("id")+"t");System.out.print(rs.getString("name")+"t");System.out.print(rs.getInt("age")+"t");System.out.println(rs.getFloat("salary"));}rs.close();stmt.close();con.close();}elseif(num==4){System.out.println("输入要更改的姓名");ResultSetrs=stmt.executeQuery("select*fromemployeeinfowherename=name");rs.last();System.out.println("输入更改后的年龄");intage=sc.nextInt();rs.updateInt("age",age);System.out.println("输入更改后的工资");floatsalary=sc.nextFloat();rs.updateFloat("salary",salary);rs.updateRow();rs.close();stmt.close();con.close();}}catch(Exceptione){e.printStackTrace();}}}}
解决方案
解决方案二:
elseif(num==2){System.out.println("请输入要删除的员工id");intid=sc.nextInt();ResultSetrs=stmt.executeQuery("select*fromemployeeinfowhereid=id");rs.last();//你把游标设置到最后一行rs.deleteRow();//然后删除,当然只能删除一行了rs.close();stmt.close();con.close();更新也是一样的
解决方案三:
汗。,。。貌似你直接用While(rs.last()){}就可以解决了。。