理解异常的基本概念;了解Java异常的层次结构;熟悉并掌握Java异常的捕获处理方法。
(1)阅读Java™ Platform, Standard Edition 8 API Specification文档,了解后续编程中将要处理的IOException及其子类FileNotFoundException、EOFException,SocketException,SQLException以及运行时异常RuntimeException与其子类IllegalStateException。
(2)根据新提供的DataProcessing类(因还未讲SQL,此类模拟异常出现情况,以一定概率随机产生异常),在所编写的Administrator、Operator和Browser类,增加异常处理功能。
import java.sql.SQLException;
import java.io.IOException;
public abstract class User {
private String name;
private String password;
private String role;
User(String name,String password,String role){
this.name=name;
this.password=password;
this.role=role;
}
public abstract void showMenu();
public void showFileList() throws SQLException{
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in accessing file DB");
System.out.println("列表……");
}
public boolean downloadFile() throws IOException{
double ranValue=Math.random();
if(ranValue>0.5) throw new IOException("Error in accessing file");
System.out.println("下载文件……");
return true;
}
public boolean changeSelfInfo(String password) throws SQLException{
if(DataProcessing.update(name,password,role)) {
this.password=password;
System.out.println("修改成功!");
return true;
}
else {
System.out.println("修改失败!");
return false;
}
}
public void exitSystem() {
System.out.println("系统退出,谢谢使用!");
System.exit(0);
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setPassword(String password) {
this.password=password;
}
public String getPassword() {
return password;
}
public void setRole(String role) {
this.role=role;
}
public String getRole() {
return role;
}
}
import java.util.Enumeration;
import java.util.Hashtable;
import java.sql.*;
public class DataProcessing {
private static boolean connectToDB=false;
static Hashtable users;
static {
users=new Hashtable();
users.put("hjy", new Operator("hjy","111","operator"));
users.put("cr", new Browser("cr","000","browser"));
users.put("myk", new Administrator("myk","250","administrator"));
Init();
}
public static void Init() {
//connect to database
//update database connection status
if(Math.random()>0.2) connectToDB=true;
else connectToDB=false;
}
public static User searchUser(String name) throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
return users.get(name);
}
return null;
}
public static User search(String name,String password) throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
User temp=users.get(name);
if(temp.getPassword().equals(password))
return temp;
}
return null;
}
public static Enumeration getAllUser() throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
Enumeration e=users.elements();
return e;
}
public static boolean update(String name,String password,String role) throws SQLException{
User user;
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
if(role.equalsIgnoreCase("administrator"))
user=new Administrator(name,password,role);
else if(role.equalsIgnoreCase("operator"))
user=new Operator(name,password,role);
else
user=new Browser(name,password,role);
users.put(name, user);
return true;
}
else return false;
}
public static boolean insert(String name,String password,String role) throws SQLException{
User user;
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name))
return false;
else {
if(role.equalsIgnoreCase("administrator"))
user=new Administrator(name,password,role);
else if(role.equalsIgnoreCase("operator"))
user=new Operator(name,password,role);
else
user=new Browser(name,password,role);
users.put(name, user);
return true;
}
}
public static boolean delete(String name) throws SQLException{
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
if(users.containsKey(name)) {
users.remove(name);
return true;
}
else return false;
}
public void disconnectFromDB() {
if(connectToDB) {
//close Statement and Connection
try {
if(Math.random()>0.5) throw new SQLException("Error in disconnecting DB");
}
catch(SQLException sqlException) {
sqlException.printStackTrace();
}finally {
connectToDB=false;
}
}
}
}
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Scanner;
public class Administrator extends User{
private Scanner sc;
Administrator(String name, String password, String role) {
super(name, password, role);
}
public void changeUserInfo() {
System.out.println("请输入要修改的用户姓名:");
String cn=sc.next();
System.out.println("请输入新的用户密码:");
String cm=sc.next();
System.out.println("请输入新的用户角色:");
String cr=sc.next();
try {
if(DataProcessing.update(cn, cm, cr)) {
System.out.println("修改用户信息成功!");
}
else System.out.println("修改失败!");
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
}
public void delUser() {
System.out.println("请输入要删除的用户姓名:");
String dn=sc.next();
try {
if(DataProcessing.delete(dn)) {
System.out.println("删除成功!");
}
else System.out.println("删除失败!该用户不存在");
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
}
public void addUser() {
System.out.println("请输入要增加的用户姓名:");
String an=sc.next();
System.out.println("请输入要增加的用户密码:");
String am=sc.next();
System.out.println("请输入要增加的用户角色:");
String ar=sc.next();
try {
if(DataProcessing.insert(an, am, ar)) {
System.out.println("增加用户"+an+"成功!");
}
else System.out.println("增加用户"+an+"失败!");
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
}
public void listUser() {
Enumeration e = null;
try {
e = DataProcessing.getAllUser();
}
catch (SQLException e1) {
System.out.println("文件访问错误"+e1.getMessage());
}
User user;
while(e.hasMoreElements()) {
user=e.nextElement();
System.out.println("姓名:"+user.getName()+"密码:"+user.getPassword()+"角色:"+user.getRole());
}
}
public void showMenu() {
System.out.println("欢迎进入档案操作员菜单!");
System.out.println("1.修改用户");
System.out.println("2.删除用户");
System.out.println("3.新增用户");
System.out.println("4.列出用户");
System.out.println("5.下载文件");
System.out.println("6.文件列表");
System.out.println("7.修改(本人)密码");
System.out.println("8.退出");
sc=new Scanner(System.in);
int i=sc.nextInt();
switch(i) {
case 1:{
changeUserInfo();
break;
}
case 2:{
delUser();
break;
}
case 3:{
addUser();
break;
}
case 4:{
listUser();
break;
}
case 5:{
try {
downloadFile();
}
catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 6:{
try {
showFileList();
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 7:{
System.out.println("请输入新密码:");
String psd=sc.next();
try {
changeSelfInfo(psd);
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 8:{
exitSystem();
}
default:{
System.out.println("输入非法!请重新输入!");
break;
}
}
showMenu();
}
}
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
public class Operator extends User{
Operator(String name, String password, String role) {
super(name, password, role);
}
public void showMenu() {
System.out.println("欢迎进入档案操作员菜单!");
System.out.println("1.上传文件");
System.out.println("2.下载文件");
System.out.println("3.文件列表");
System.out.println("4.修改密码");
System.out.println("5.退出");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int i=sc.nextInt();
switch(i) {
case 1:{
uploadFile();
break;
}
case 2:{
try {
downloadFile();
}
catch (IOException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 3:{
try {
showFileList();
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 4:{
System.out.println("请输入新密码:");
String psd=sc.next();
try {
changeSelfInfo(psd);
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 5:{
exitSystem();
}
default:{
System.out.println("输入非法!请重新输入!");
break;
}
}
showMenu();
}
public void uploadFile() {
//上传文件
System.out.println("上传成功!");
}
}
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
public class Browser extends User{
Browser(String name, String password, String role) {
super(name, password, role);
}
public void showMenu() {
System.out.println("欢迎进入档案浏览员菜单!");
System.out.println("1.下载文件");
System.out.println("2.文件列表");
System.out.println("3.修改密码");
System.out.println("4.退出");
System.out.print("请选择:");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int i=sc.nextInt();
switch(i) {
case 1:{
try {
downloadFile();
}
catch (IOException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 2:{
try {
showFileList();
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 3:{
System.out.println("请输入新密码:");
String psd=sc.nextLine();
try {
changeSelfInfo(psd);
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 4:{
exitSystem();
}
default:{
System.out.println("输入非法!请重新输入!");
break;
}
}
showMenu();
}
}
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
while(true) {
System.out.println("欢迎进入档案管理系统!");
System.out.println("1.登录");
System.out.println("2.退出");
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1) {
System.out.println("请输入用户名:");
String name=sc.next();
try {
if(DataProcessing.searchUser(name)!=null) {
System.out.println("请输入密码:");
String password=sc.next();
try {
if(DataProcessing.search( name ,password)!=null)
DataProcessing.search(name, password).showMenu();
else System.out.println("密码错误!");
}
catch(SQLException e) {
System.out.println("数据库错误"+e.getMessage());
}
}
else {
System.out.println("用户不存在!");
}
}
catch (SQLException e) {
System.out.println("数据库错误"+e.getMessage());
}
}
else if(i==2) {
System.exit(0);
}
else {
System.out.println("输入非法!请重新输入。");
}
}
}
}
以下是需要完成的结果展示:Java面向对象与多线程综合实验(二)之 异常处理