📄 agendaservice.java
字号:
} }catch (SQLException e) { System.out.println( "查询时出错 " ); System.out.println( "出错信息: " + e.getMessage() ); } try{//将新建会议写入数据库 stmt.executeUpdate("insert into meeting (预约用户,开始时间,结束时间,会议标识,创建者) values ('"+command[3]+"','"+inStart+"','"+inEnd+"','"+command[6]+"','"+command[1]+"')"); rs=stmt.executeQuery("select * from meeting"); rs.next(); System.out.println("会议添加成功"); } catch(SQLException e){ System.out.println( "执行添加时出错 !! " ); System.out.println( "出错信息: " + e.getMessage() ); } } close(); } void query() throws ParseException{//查询用户在一时断内的所有会议 String inStart=turnIntoDate(command[3]);//将输入的时间字符串格式化 String inEnd=turnIntoDate(command[4]);//将输入的时间字符串格式化 String other,start,end,title,user; int total=0;//保存符合条件的数码 connecte(); if(checkUserName()){//用户名存在,且密码正确 try { rs = stmt.executeQuery("select * from meeting"); System.out.println("查询结果为"); System.out.println("*******************************************************************************************"); System.out.println("* 预约用户 开始时间 结束时间 会议标识 创建者 *"); System.out.println("*******************************************************************************************"); while(rs.next()){//查询 other=rs.getString("预约用户"); start=rs.getString("开始时间"); end=rs.getString("结束时间"); title=rs.getString("会议标识"); user=rs.getString("创建者"); if(other.equals(command[1])||user.equals(command[1])){ if(timeConflict(inStart,inEnd,start,end)){//打印符合条件的记录 System.out.println("* "+other+" "+start+" "+end+" "+title+" "+user); total++; } } } System.out.println("共有: "+total+" 项记录符合"); } catch( SQLException e ) { System.out.println( "执行查询时出错 !! " ); System.out.println( "出错信息: " + e.getMessage() ); } } close(); } void delete() throws SQLException{//删除某一条记录 connecte(); if(checkUserName()){ int i=0; try{ i = stmt.executeUpdate("delete from meeting where 创建者='"+command[1]+"' and 会议标识='"+command[3]+"'"); } catch(SQLException e){ System.out.println( "删除是出错 !! " ); System.out.println( "出错信息: " + e.getMessage() ); } conn.commit(); if(i==1) System.out.println("已经成功删除会议"); else System.out.println("改会议不存在"); } close(); } void clearAll(){//删除所有用户的会议记录 connecte(); if(checkUserName()){ int i=0; try{ i = stmt.executeUpdate("delete from meeting where 创建者='"+command[1]+"'"); conn.commit(); } catch (SQLException e) { System.out.println( "删除是出错 !! " ); System.out.println( "出错信息: " + e.getMessage() ); } if(i==1){ System.out.println( "已经成功删除所有会议" ); } else{ System.out.println( "会议不存在 " ); } close(); } } void batch(String fileName) throws SQLException, ParseException{//文本读入、进行批处理 Scanner sc = null; try {//从文本读入命令 sc = new Scanner(new BufferedReader(new FileReader(fileName))); } catch (FileNotFoundException ex) { System.out.println("文件读入时出错"); } String temp; Scanner s = null; while (sc.hasNext()) { temp=sc.nextLine();//获取一条命令 if(temp.equals(""))continue;//忽略空白命令 s=new Scanner(temp); int i=0; while(s.hasNext())//处理输入的命令 { command[i]=s.next(); if(command[i].equals("")) continue; i++; } lenght=i; s.close(); if(command[0].equals("batch")){//过滤掉输入命令中的batch命令,防止死循环 System.out.println("进行批处理时,不能执行batch命令"); quit(); } analyze();//执行操作 } sc.close(); } void quit(){//退出程序 System.out.println("程序已关闭"); System.exit(0); } String turnIntoDate(String s){//将输入的时间字符串格式化,如2009-03-01/10:00转换为2009-03-01 10:00:00 StringBuffer sb=new StringBuffer(); for(int i=0;i<s.length();i++){ if(s.charAt(i)=='/')sb.append(' '); else sb.append(s.charAt(i)); } sb.append(":00"); String temp=new String(sb); return temp; } boolean timeConflict(String inputstart,String inputend,String readstart,String readend) throws ParseException { boolean conflict=false; DateFormat df=DateFormat.getDateTimeInstance(); /* Date inputstart1 = null,inputend1 = null,readstart1 = null,readend1 = null; try{//将字符传转换为日期类型; inputstart1= (Date) df.parse(inputstart); inputend1=(Date) df.parse(inputend); readstart1=(Date) df.parse(readstart); readend1=(Date) df.parse(readend); }catch(ParseException e){ System.out.println("输入日期格式有误"); }*/ try { if((df.parse(inputstart).before(df.parse(readend))&&df.parse(inputend).after(df.parse(readend))) ||(df.parse(inputstart).after(df.parse(readstart))&&df.parse(inputend).before(df.parse(readend)))) /* if((inputstart1.before(readend1)&&inputend1.after(readend1)) ||inputstart1.after(readstart1)&&inputend1.before(readend1))//判断两个时间段之间是否有交叉时段*/ conflict=true; } catch(ParseException e) { System.out.println("输入日期格式有误"); } return conflict; } boolean checkUserName(){//判断用户名是否存在,并且检测其密码输入的正确性 sql = "select * from person"; boolean exist=false; boolean rightP=false; try{ rs = stmt.executeQuery( sql ); //从结果集中取出数据 while( rs.next() ) { String str=rs.getString("用户名"); if(str.equals(command[1])){ exist=true; if(rs.getString("密码").equals(command[2])){ rightP=true; return true; } } } } catch( SQLException e ) { System.out.println( "执行查询时出错 !! " ); System.out.println( "出错信息: " + e.getMessage() ); } if(!exist) System.out.println("用户名不存在"); if(!rightP) System.out.println("密码错误"); return false; } int func;// int lenght; private String sql; String command[]=new String[20];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -