解读MySQL驱动加载逻辑
我们很早之前就知道最基础的JDBC编写,先执行Class.forName方法,加载MySQL驱动。但是为什么加载过驱动后,后续的接口层的调用就会自动切换到MySQL的相关代码去执行呢?(作者:高元)
常见jdbc编写
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/gdjt_db","root","root");
ps = connection.prepareStatement("select * from sys_user where id = ? ");
ps.setString(1, "1");
rs = ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString("name"));
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(rs != null)rs.close();
}catch (E