杨花落尽子规啼
2018-11-29 17:07
为啥每个单独的步骤执行就可以,组合在一起就有问题 ,还有更新两次 问题:第二次更新时更新不动,增加完 新信息,按其他按钮 不动。安老师写的增加代码。
我的代码:
package com.Demo.view;
import com.Demo.action.GoddessAction;
import com.Demo.model.Goddess;
import org.omg.Messaging.SYNC_WITH_TRANSPORT;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class View {
private static final String CONTEXT="欢迎来到女生区:\n"+
"下面是女生区的功能列表:\n"+
"[MAIN/M]:主菜单\n"+
"[QUERY/Q]:查看全部女生信息\n"+
"[GET/G]:查看某位女生详细信息\n"+
"[ADD/A]:添加女生信息\n"+
"[UPDATE/U]:更新女生信息\n"+
"[DELETE/D]:删除女生信息\n"+
"[SEARCH/S]:查询女生信息(根据姓名、手机号来查询)\n"+
"[EXIT/E]:退出女生区:\n"+
"[BREAK/B]:退出当前功能,返回主菜单";
private static final String OPERATION_MAIN="MAIN";
private static final String OPERATION_QUERY="QUERY";
private static final String OPERATION_GET="GET";
private static final String OPERATION_ADD="ADD";
private static final String OPERATION_UPDATE="UPDATE";
private static final String OPERATION_DELETE="DELETE";
private static final String OPERATION_SEARCH="SEARCH";
private static final String OPERATION_EXIT="EXIT";
private static final String OPERATION_BREAK="BREAK";
public static void main(String[] args) throws ParseException {
System.out.println(CONTEXT);
Scanner scanner=new Scanner(System.in);
Goddess goddess=new Goddess();
GoddessAction goddessAction=new GoddessAction();
String pernious=null;
Integer step=1;
Integer step1=1;
Integer step2=1;
Integer step3=1;
Integer step4=1;
List<Map<String,Object>> params= new ArrayList<Map<String, Object>>();
Map<String,Object>param=new HashMap<String, Object>();
//保持程序一直运行
while(scanner.hasNext()){
String input=scanner.next().toString();
if(OPERATION_EXIT.equals(input.toUpperCase())
||OPERATION_EXIT.substring(0,1).equals(input.toUpperCase())){
System.out.println("您已成功退出女生区!");
break;
}else if(OPERATION_ADD.equals(input.toUpperCase())
||OPERATION_ADD.substring(0,1).equals(input.toUpperCase())
||OPERATION_ADD.equals(pernious)){
pernious=OPERATION_ADD;
//增加女生
if(step==1){
System.out.println("请输入女生的[姓名]");
}else if(step==2){
goddess.setUser_name(input);
System.out.println("请输入女生的[年龄]");
}else if(step==3){
goddess.setAge(Integer.valueOf(input));
System.out.println("请输入女生的[生日],如YYYY-MM-DD");
}else if(step==4){
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-DD");
Date birthday=null;
try{
birthday=simpleDateFormat.parse(input);
goddess.setBirthday(birthday);
System.out.println("请输入女生[邮箱]");
}catch (ParseException e){
e.printStackTrace();
System.out.println("您输入的格式有误,请重新输入");
step=3;
}
}else if(step==5){
goddess.setEmail(input);
System.out.println("请输入女生[手机号]");
}else if(step==6){
goddess.setMobile(input);
try {
goddessAction.add(goddess);
System.out.println("新增女生成功。");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("新增女生失败!");
}
}
if(OPERATION_ADD.equals(pernious)){
step++;
}
}else if(OPERATION_QUERY.equals(input.toUpperCase())
||OPERATION_QUERY.substring(0,1).equals(input.toUpperCase())){
try {
List<Goddess>goddesses=goddessAction.query();
for (Goddess goddess1:goddesses){
System.out.println(
"ID:"+goddess1.getId()+" "+
"姓名:"+goddess1.getUser_name()+" "+
"年龄:"+goddess1.getAge()+" "+
"邮箱:"+goddess1.getEmail()+" "+
"电话:"+goddess1.getMobile());
}
} catch (SQLException e) {
e.printStackTrace();
}
}else if(OPERATION_DELETE.equals(input.toUpperCase())
||OPERATION_DELETE.substring(0,1).equals(input.toUpperCase())
||OPERATION_DELETE.equals(pernious)){
pernious=OPERATION_DELETE;
if(step1==1){
try {
Goddess idList=null;
System.out.println("请输入要删除的女生ID:");
Integer out=scanner.nextInt();
for(int i=0;i<goddessAction.query().size();i++){
}
goddessAction.delete(out);
System.out.println("删除成功!");
} catch (Exception e) {
System.out.println("输入的格式有误,请重新输入[数字]:");
step=0;
}
}
if(OPERATION_DELETE.equals(pernious)){
step1++;
}
}else if(OPERATION_GET.equals(input.toUpperCase())
||OPERATION_GET.substring(0,1).equals(input.toUpperCase())
||OPERATION_GET.equals(pernious)){
pernious=OPERATION_GET;
if(step2==1){
try {
System.out.println("请输入要查看女生的ID:");
Integer in=scanner.nextInt();
Goddess goddess2=goddessAction.get(in);
System.out.println(goddess2.toString());
//continue;
}catch (Exception e) {
System.out.println("您输入的女生ID格式有误,格式[数字]");
step2=0;
}
}
if(OPERATION_GET.equals(pernious)){
step2++;
}
}else if(OPERATION_SEARCH.equals(input.toUpperCase())
||OPERATION_SEARCH.substring(0,1).equals(input.toUpperCase())){
pernious=OPERATION_SEARCH;
if(step3==1) {
try {
List<Goddess> goddesses = null;
System.out.println("请输入女生的姓名:");
String name = scanner.next();
System.out.println("请输入女生的手机号:");
String mobile = scanner.next();
param = new HashMap<String, Object>();
param.put("name", "user_name");
param.put("rela", "=");
String realName = "'" + name + "'";
param.put("value", realName);
params.add(param);
param = new HashMap<String, Object>();
param.put("name", "mobile");
param.put("rela", "=");
String realMobile = "'" + mobile + "'";
param.put("value", realMobile);
params.add(param);
goddesses = (List<Goddess>) goddessAction.query(params);
for (int i = 0; i < goddesses.size(); i++) {
System.out.println("查询到的信息为:");
System.out.println(goddesses.get(i).toString());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("未查到任何相关信息,请重新输入");
step3=0;
}
}
if(pernious.equals(OPERATION_SEARCH)){
step3++;
}
}else if(OPERATION_MAIN.equals(input.toUpperCase())
||OPERATION_MAIN.substring(0,1).equals(input.toUpperCase())){
System.out.println(CONTEXT);
}else if(OPERATION_BREAK.equals(input.toUpperCase())
||OPERATION_BREAK.substring(0,1).equals(input.toUpperCase())){
System.out.println("你已退出女生区!");
break;
}else if(OPERATION_UPDATE.equals(input.toUpperCase())
||OPERATION_UPDATE.substring(0,1).equals(input.toUpperCase())
||OPERATION_UPDATE.equals(pernious)) {
pernious=OPERATION_UPDATE;
if(step4==1){
try {
System.out.println("请输入要修改女生的ID:");
Integer inId=scanner.nextInt();
Goddess goddess2=goddessAction.get(inId);
System.out.println(goddess2.toString());
goddess.setId(inId);
step4=2;
}catch (Exception e) {
System.out.println("您输入的女生ID格式有误,格式[数字]");
step=0;
}
}
if(step4==2){
System.out.println("请输入更改后女生的姓名:");
}else if(step4==3){
goddess.setUser_name(input);
System.out.println("请输入更改后女生年龄:");
}else if(step4==4){
goddess.setAge(Integer.valueOf(input));
System.out.println("请输入改后女生生日:");
}else if(step4==5){
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-DD");
Date birthday=null;
try{
birthday=simpleDateFormat.parse(input);
goddess.setBirthday(birthday);
System.out.println("请输入女生[邮箱]");
}catch (ParseException e){
e.printStackTrace();
System.out.println("您输入的格式有误,请重新输入");
step4=4;
}
}else if (step4==6){
goddess.setEmail(input);
System.out.println("请输入更改后女生手机号:");
}else if (step4==7){
goddess.setMobile(input);
System.out.println("请输入更新者");
}else if(step4==8){
goddess.setUpdate_user(input);
try {
goddessAction.update(goddess);
System.out.println("更新成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
if(OPERATION_UPDATE.equals(pernious)){
step4++;
}
}else {
System.out.println("您输入的方式有误");
System.out.println(CONTEXT);
}
}
}
}
新增成功,将对应的step重置为1;
JDBC之 “ 对岸的女孩看过来”
99334 学习 · 916 问题
相似问题