catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("驱动类加载失败。"); }
时间: 2024-05-31 13:14:29 浏览: 14
这是一个Java中的异常处理代码块。如果在程序运行时无法加载指定的类,就会抛出ClassNotFoundException异常。在这段代码中,如果出现了这个异常,程序会打印出异常信息并输出"驱动类加载失败。"的提示。同时,使用e.printStackTrace()可以输出更详细的异常信息,有助于调试问题。
相关问题
try { Class.forName(driverClass); //out.print("连接成功"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("数据库驱动加载错误!"); }
这段代码是用来加载数据库驱动的。首先使用Class.forName()方法加载指定的驱动类,如果该类不存在,则会抛出ClassNotFoundException异常。如果成功加载了驱动类,则可以使用该驱动来连接数据库。在这段代码中,如果加载驱动类出现异常,则会打印错误信息,并提示“数据库驱动加载错误!”
在测试类中能用switch case语句实现其在键盘上输入得到对应结果的方法public static void savePersonList(List<Person> list, String fileName) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)); oos.writeObject(list); oos.close(); System.out.println("保存成功!"); } catch (IOException e) { System.out.println("保存失败!"); e.printStackTrace(); } } public static List<Person> loadPersonList(String fileName) { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); List<Person> list = (List<Person>) ois.readObject(); ois.close(); System.out.println("读入成功!"); return list; } catch (IOException | ClassNotFoundException e) { System.out.println("读入失败!"); e.printStackTrace(); return new ArrayList<>(); } }
可以使用switch case语句实现在键盘上输入得到对应结果的方法,例如:
```
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入操作类型:1-保存数据,2-读取数据");
int type = input.nextInt();
switch (type) {
case 1:
List<Person> list = new ArrayList<>();
// 添加数据到list中
savePersonList(list, "person.dat");
break;
case 2:
List<Person> resultList = loadPersonList("person.dat");
// 处理读取到的数据
break;
default:
System.out.println("输入无效!");
break;
}
}
public static void savePersonList(List<Person> list, String fileName) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(list);
oos.close();
System.out.println("保存成功!");
} catch (IOException e) {
System.out.println("保存失败!");
e.printStackTrace();
}
}
public static List<Person> loadPersonList(String fileName) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
List<Person> list = (List<Person>) ois.readObject();
ois.close();
System.out.println("读入成功!");
return list;
} catch (IOException | ClassNotFoundException e) {
System.out.println("读入失败!");
e.printStackTrace();
return new ArrayList<>();
}
}
}
```
在上面的代码中,通过Scanner类获取用户输入的操作类型,然后使用switch case语句来执行不同的操作。当用户输入1时,调用savePersonList方法保存数据;当用户输入2时,调用loadPersonList方法读取数据;否则输出“输入无效!”。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)