1. try catch finally 返回的是finally的值
package com.easy;
import java.util.*;
public class model {
public static void main(String args[]){
int x = test();
System.out.println(x);
}
@SuppressWarnings("finally")
public static int test(){
int a=0;
try{
a = 2/0;
return a;
}
catch(Exception e){
return 1;
}
finally{
return 2;
}
}
}
打印值为2
2. 在eclipse中与mysql进行连接
package com.easy;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC {
public static void main(String args[]){
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 创建连接
String url="jdbc:mysql://localhost:3306/mysql";
String user="root";
String password="root";
// 创建执行对象con
Connection con = DriverManager.getConnection(url, user, password);
Statement sta = con.createStatement();
// 定义sql语句
String sql ="SELECT now() AS time";
ResultSet rs = sta.executeQuery(sql);
// 解析结果集合
while(rs.next()){
String time = rs.getString("time");
System.out.println(time);
}
//抛出两个异常
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}