📄 usedrivermanager.java
字号:
package ch13;
import java.sql.*;
public class UseDriverManager {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost/sql_test";
String userName = "root";
String password = "root";
Connection conn = null;
try {
System.out.println("第一次连接数据库之前");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("第一次连接数据库之后");
} catch(SQLException e) {
System.out.println("第一次连接数据库的过程中出现SQL异常");
}
if (conn==null)
System.out.println("第一次连接数据库失败");
else
System.out.println("第一次连接数据库成功");
try {
System.out.println("\n加载驱动器类之前");
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动器类之后");
} catch(ClassNotFoundException e) {
System.out.println("加载驱动器类时出现异常");
}
try {
conn = null;
System.out.println("\n第二次连接数据库之前");
conn = DriverManager.getConnection(url, userName, password);
System.out.println("第二次连接数据库之后");
} catch(SQLException e) {
System.out.println("第一次连接数据库的过程中出现SQL异常");
}
if (conn==null)
System.out.println("第二次连接数据库失败");
else
System.out.println("第二次连接数据库成功");
try {
conn.close();
} catch (SQLException e) {
System.out.println("关闭数据库连接时出现SQL异常");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -