JAVA期末复习总结
对JAVA中五个不熟悉的部分做一个知识总结
StringBuffer类
异常类
线程
IO
数据库操作
StringBuffer
1、以下是 Character(char)类支持的主要方法:
序号 | 方法 | 描述 |
---|---|---|
1 | isLetter() | 是否是一个字母 |
2 | isDigit() | 是否是一个数字字符 |
3 | isWhitespace() | 是否是一个空白字符 |
4 | isUpperCase() | 是否是大写字母 |
5 | isLowerCase() | 是否是小写字母 |
6 | toUpperCase() | 指定字母的大写形式 |
7 | toLowerCase() | 指定字母的小写形式 |
8 | toString() | 返回字符的字符串形式,字符串的长度仅为1 |
2、以下是 StringBuffer 类支持的主要方法:
序号 | 方法 | 描述 |
---|---|---|
1 | public StringBuffer append(String s) | 将指定的字符串追加到此字符序列。 |
2 | public StringBuffer reverse() | 将此字符序列用其反转形式取代。 |
3 | public delete(int start, int end) | 移除此序列的子字符串中的字符。 |
4 | public insert(int offset, int i) | 将 int 参数的字符串表示形式插入此序列中。 |
5 | replace(int start, int end, String str) | 使用给定 String 中的字符替换此序列的子字符串中的字符 |
3、下面的列表里的方法和 String 类的方法类似:
序号 | 方法 | 描述 |
---|---|---|
1 | char charAt(int index) | 返回此序列中指定索引处的 char 值。 |
2 | int length() | 返回长度(字符数)。 |
3 | void setCharAt(int index, char ch) | 将给定索引处的字符设置为 ch。 |
4 | String toString() | 返回此序列中数据的字符串表示形式。 |
5 | int indexOf(String str) | 返回第一次出现的指定子字符串在该字符串中的索引。 |
6 | int indexOf(String str, int fromIndex) | 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 |
7 | CharSequence subSequence(int start, int end) | 返回一个新的字符序列,该字符序列是此序列的子序列。 |
8 | String substring(int start, int end) | 返回一个新的 String,它包含此序列当前所包含的字符子序列。 |
9 | int indexOf(String str) | 返回第一次出现的指定子字符串在该字符串中的索引。 |
补充String的一些方法:
序号 | 方法 | 描述 |
---|---|---|
1 | String replace(char oldChar, char newChar) | 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 |
2 | 45 String trim() | 返回字符串的副本,忽略前导空白和尾部空白。 |
3 | isEmpty() | 判断字符串是否为空。 |
Example
编写一个程序,将字符串Uav3K5h09AsjhaO中的大写字母转换成小写字母,小写字母转换成大写字母,同时删除字符串中的数字。
异常类
待写…
线程-----主要内容是线程的创建
1、Thread类的构造方法有三种:
public Thread( )
public Thread(Runnable target)
public Thread(String name)
2、根据构造方法的不同,创建线程有两种方式
方式一:定义Thread类的子类,使用默认构造方法创建线程
public class TeatThread {
public static void main(String[] args){
MyThread mythread = new MyThread();
mythread.start();
}
}
class MyThread extends Thread{
@overwrite
public void run(){
System.out.println("This is my thread.");
}
}
方式二:实现Runnable接口,使用public Thread(Runnable target)构造方法创建线程
public class TestThread{
public static void main(){
MyTarget mytarget = new MyTarget();
Thread thread = new Thread(mytarget);
thread.start();
}
}
class MyTarget implements Runnable{
@overwrite
public void run(){
System.out.println("This is my thread.");
}
}
3、线程的生命周期
创建 运行 中断 死亡
I/O
System控制台输入和输出
Scanner scan = new Scanner(System.in);
String str1 = scan.next();
String str2 = scan.nextLine();
int i = scan.nextInt();
scan.hasNextInt();//判断的当前输入是否是int类型
System.out.println(String.format("%.2f",f));//按照指定格式输出
File类:
File类的三种构造方法:
File flie = new File(String filename); 要求该文件与应用程序在同一目录下
File file = new File(String directoryPath,String filename);
File file = new File(File f,String filename);
public long length();获取文件长度,单位是字节
文件字节流:
文件字节输入流 FileInputStream,是字节输入流InputStream的子类
构造方法:
FileIntputStream(String filename);
FileIntputStream(File file);
读从输入流中读取字节的方法
int read(); //每次读取一个字节
int read(tybe b[]);//从输入流中读b.length()个字节,并保存到数组b[]当中
int read(tybe b[], int off, int len);
文件字节输出流FileOutputStream,是字节输出流OutputStream的子类
构造方法:
FileOutputStream(String filename);
FileOutputStream(File file);
FileOutputStream(String filename, boolean append);
FileOutputStream(File file, boolean append);
当append为true时,输出流不会刷新所指向的文件
public void write(byte b[]);//将数组b[]中的数据写入到输出流当中
public void write(byte b[], int off, int len);
try{
...
}catch(IOException e){
System.ount.println(e);
}
文件字符流
文件字符输入流FileReader,是字符输入流Reader的子类
构造方法:
FileReader(String filename);
FileReader(File file);
int read();
int read(char b[]);
int read(char b[], int off, int len);
文件字符输出流FileWriter,是字符输出流Writer的子类
构造方法:
FileWriter(String filename);
FileWriter(File file);
FileWriter(String filename, boolean append);
FileWriter(File file,boolean append);
当append为true时,输出流不会刷新所指向的文件
public void write(char b[]);
public void write(char b[], int off, int len);
void write(String str);
void write(String str, int off, int len);
缓冲流【缓冲流的指向必须是一个字符流,成为缓冲流的底层流】
缓冲输入流类BufferedReader
构造方法:
BufferedReader(FileReader fr);
FileReader对象把数据从输入流中读入到缓冲区,BufferedReader对象再从缓冲区读出
readLine();
缓冲输出流BufferedWriter
构造函数:
BufferedWriter(FileWriter fw);
FileWriter对象把数据写入到缓冲区,再由BufferedWriter对象把数据从缓冲区写入到目的地
write(String str);
write(String str, int off, int len);
BufferedWriter对象调用newLine()
;方法向文件写入一个换行,调用flush可以刷新缓冲区 ,即将缓冲区中暂存的字符写入到目的地。
System.in的使用
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
Example_9-5
public class CopyFile{
try{
FileReader fr = new FileReader("1.txt");
BuffferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("2.txt");
BufferedWriter bw = new BufferedWriter(fw);
String s = null;
int i=0;
while((s = br.readLine()) != null){
i++;
bw(r + " " + s);
bw.newLine()
}
fr.close();
br.close();
fw.close();
bw.close();
fr = new FileReader("2.txt");
br = new BufferedReader(fr);
while((s = br.readLine()) != null){
System.out.println(s);
}cathch(IOException e){
System.out.println(e);
}
}
数据库操作
此部分内容来源:https://www.w3cschool.cn/jdbc/
Java应用程序使用JDBC-ODBC桥接器访问数据库有三个步骤:
建立JDBC-ODBC桥接器
创建ODBC数据源
和ODBC数据源建立连接
建立JDBC-ODBC桥接器
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
try{
class.forName(JDBC_DRIVER);
}catch(ClassNotFoundException e){
System.out.println(e);
}
创建ODBC数据源
在数据库中创建table不是Java所关心的,因此我们着重关注1、3两步
和ODBC数据源建立连接
String DB_URL = "jdbc:mysql://127.0.0.1:3306/test";//数据源地址
USER = "root";
PASS = "******"
try{
Connection conn = DriverManager.getConnection(DB_URl, USER,PASS);
}catch(SQLException e){
System.out.println(e);
}
完整的代码
Stirng JDBC_DRIVER = "com.mysql.jdbc.Driver";
Sting DB_URL = "jdbc:mysql://127.0.0.1:3306/test";
String USER = "root";
String PASS = "******"
try{
class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
}catch(ClassNotFountException e){
System.out.println(e);
}catch(SQLException e){
System.out.println(e);
}
建立连接后的可以与数据库进行交互
使用Statement接口定义的方法和属性,可以让你发送 SQL 命令到数据库,并从你的数据库接收数据。
try{
Statement stmt = conn.createStatement();
}catch(SQLException){
System.out.println(e);
}
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery、executeUpdate 和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。其中executeQuery、executeUpdate是常用的两种:
方法 executeQuery 用于产生单个结果集的语句,例如 SELECT 语句。
方法 executeUpdate 用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句
SELECT
ResultSet rs = stmt.executeQuery("select id, name from student");
INSERT
stmt.executeUpdate("insert into student (ID, NAME, GENDER, ADDRESS) values(10, 'peter', '男', '黑龙江')");
UPDATA
stmt.executeUpdate("update student set ADDRESS = '河南' where name = '李四'");
DELETE
stmt.executeUpdate("delete from student where ID='10'");
java.sql.ResultSet 用于存储从SQL select语句返回的结果
ResultSet导航
我们可以使用ResultSet接口中的以下方法来移动光标。
方法 | 描述 |
---|---|
beforeFirst() | 将光标移动到第一行之前 |
afterLast() | 将光标移动到最后一行之后 |
first() | 将光标移动到第一行 |
ast() | 将光标移动到最后一行 |
absolute(int row) | 将光标移动到指定的行 |
relative(int row) | 相对于光标所在位置向前或向后移动光标行数。 |
previous() | 将光标移到上一行。 |
next() | 将光标移动到下一行。 |
int getRow() | 返回游标指向的行号。 |
moveToInsertRow() | 将光标移动到我们可以将新行插入数据库的位置。当前行号不更改。 |
moveToCurrentRow() | 如果光标当前位于插入行,则将光标移回当前行;否则,此方法不执行任何操作。 |
获取ResultSet列数据
我们有两种方法在ResultSet中获取数据:
①按列索引
②按列名称
例如,以下两个方法从列中获取int值。第一个是按列名称,第二个是按列索引。
public int getInt(String columnName)
public int getInt(int columnIndex);
遍历ResultSet获取查询结果中的所有值
while(re.next()){
int id = re.getInt("id");
String name = re.getString("name");
System.out.println("学号:" + id + " " + "姓名:" + name);
}
更新ResultSet
我们可以更新ResultSet对象中的当前行。
我们需要在更新期间指明列名或索引。
例如,要更新当前行的String列,我们可以使用以下方法。
public void updateString(int columnIndex, String s) throws SQLException
public void updateString(String columnName, String s) throws SQLException
public void updateInt/Long/Double/String/Boolean/Date
将修改的ResultSet结果推送到数据库,请调用以下方法之一。
方法 | 描述 |
---|---|
updateRow() | 更新数据库中的相应行。 |
deleteRow() | 从数据库中删除当前行。 |
refreshRow() | 刷新结果集以反映数据库中的任何更改。 |
cancelRowUpdates() | 取消对当前行所做的任何更新。 |
insertRow() | 当光标指向插入行时,在数据库中插入一行。 |
▲教材第四版【例12-1】【例12-3】【例12-4】
补充Number类和Math类相关方法
补充math类
1 round() 它表示四舍五入,算法为 Math.floor(x+0.5),
即将原来的数字加上 0.5 后再向下取整,所以,Math.round(11.5) 的结果为12,Math.round(-11.5) 的结果为-11。
2 min() 返回两个参数中的最小值。
3 max() 返回两个参数中的最大值。
4 pow() 返回第一个参数的第二个参数次方。
5 sqrt() 求参数的算术平方根。
6 random() 返回一个随机数。
补充Number类
1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回。
Integer i = new Integer(5);
int x = i.intvalue();
2 equals() 判断对象是否与参数相等。
“==”与“equals()”的比较:
==是判断两个变量或实例是不是指向同一个内存空间
而equals()的使用根据对象的类型不同而有所差异,对于基本数据类型,或Number、String类来说,equals()是判断两个变量或实例所指向的内存空间的值是不是相同。
对于其他类的对象来说,若要使用equals()方法,需要对equals()进行重写。
java中equals()的源码:
public boolean equals(Object obj) {
return (this == obj);
}