Java中级

Java中级

1.异常

1.分类

  1. Error:Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。比如:StackOverflowError和OOM。一般不编写针对性的代码进行处理。
  2. Exception:其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
异常的体系结构
* java.lang.Throwable
* |-----java.lang.Error:一般不编写针对性的代码进行处理。
* |-----java.lang.Exception:可以进行异常的处理
* |------编译时异常(checked)不会生成字节码文件
* |-----IOException
* |-----FileNotFoundException
* |-----ClassNotFoundException
* |------运行时异常(unchecked,RuntimeException)
* |-----NullPointerException//空指针异常
* |-----ArrayIndexOutOfBoundsException//数组角标越界
* |-----ClassCastException//类型转化异常
* |-----NumberFormatException//编码格式异常
* |-----InputMismatchException//输入不匹配
* |-----ArithmeticException//算术异常

2.处理异常方式

throws

try-catch-finally

finally中声明的是一定会被执行的代码。即使catch中又出现异常了,try中return语句,catch中return语句等情况。

3.自定义异常

1
2
3
4
5
6
7
8
9
10
public class MyException extends Exception{

private static final long serialVersionUID = 1L;

public MyException() {}

public MyException(String msg) {
super(msg);
}
}

2.JDBC

1.定义

JDBC的全称是java数据库链接(Java Data Base Connect),它是用于执行SQL语句的java代码,应用程序可以通过JDBC链接到数据库,并使用SQL语句来完成对数据库中数据的新增、查询、删除和更新等操作

PreparedStatement的优点-防止SQL注入式攻击

PreparedStatement有预编译机制,性能比Statement更快

PreparedStatement 使用参数设置,可读性好,不易犯错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static void main(String[] args) {

Connection c = null;
Statement s = null;
try {
// 初始化驱动
Class.forName("com.mysql.jdbc.Driver");
// 建立与数据库的Connection连接
c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/a?characterEncoding=UTF-8", "root", "admin");
// 创建PreparedStatement
PreparedStatement ps = c.prepareStatement(sql);
// SQL语句
String sql = "insert into cc values(?,?)";
ps.setString(1, "aaa");
ps.setString(1, "bbb");
// 执行SQL
ps.execute();

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 数据库的连接时有限资源,相关操作结束后,养成关闭数据库的好习惯
// 先关闭PreparedStatement
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 后关闭Connection
if (c != null)
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

3.I/O

  • 根据处理数据类型的不同分为:字符流和字节流
  • 根据数据流向不同分为:输入流和输出流
  • 读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

  • 处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据

    所有的IO操作都在java.io包之中进行定义,而且整个java.io包实际上就是五个类和一个接口:
    (1)五个类:File、InputStream、OutputStream、Reader、Wirter;
    (2)一个接口:Serializable。

    只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。

1.File

没啥好补充的

2.字节流与字符流

(1)字节操作流:OutputStream、InputStream;
(2)字符操作流:Writer、Reader。

字节流

OutputStream和InputStream是字节流的两个顶层父类。让他们提供了输出流类和输入流类通用API,字节流一般用于读写二进制数据,如图像和声音数据。

OutputStream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void Test2() throws IOException {
System.out.println("开始创建文件...");
// 定义文件路径
File file = new File("D:" + File.separator + "java" + File.separator + "test" + File.separator
+ UUID.randomUUID().toString() + ".txt");
// 判断路径存在?
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 文件存在?
if (file.exists()) { // 文件存在
file.delete(); // 删除文件
} else { // 文件不存在
file.createNewFile(); // 创建新文件
}
// 实例化输出
FileOutputStream fos = new FileOutputStream(file);
String data = "哈哈哈哈哈哈哈哈哈";
System.out.println("开始生成数据源..." + file.getAbsolutePath());
// 输出数据 转化为字节数组输出
fos.write(data.getBytes());
// 关闭资源
fos.close();
System.out.println("输出流演示完成!");
}
InputStream

字符流

字符输入流writer

由于软件问题,这部分内容被软件吞了,后面从新来写

小唠嗑:

本章到这里就结束了,谢谢耐心看到这里的各位Boss,如果觉得有哪里说的不好的地方,还请高抬贵手多多原谅,不惜指教。

最后,谢谢!

---本文结束感谢您的阅读!---