import java.sql.*;
public class Demo01 {
public static void main(String[] args) {
//获得数据库的连接
//1.用户名
String name = "root";
//2.密码
String password = "2510.62";
//3.地址
String url = "jdbc:mysql://localhost/demo?useSSL=true&characterEncoding=utf8";
//4.驱动,从外部引进mysql-connector-jar文件
String driver = "com.mysql.jdbc.Driver";
//5.jdk java.sql.*
try {
//5.1加载数据库驱动,注册驱动 初始化驱动 加载jvm
Class.forName(driver); //Class类,获得当前正在加载的类或接口的字节码文件
//forName()初始化一个实例到jvm中
//5.2通过DriverManager来获得数据库的连接 Connection
Connection conn = DriverManager.getConnection(url, name, password);
System.out.println(conn);
Statement sta = conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("路径有误");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("获得连接失败");
}
}
}