1: 注册驱动
Class.forName("com.mysql.jdbc.Driver") ;
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
2: 获取数据库连接对象
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "1234") ;
jdbc: mysql:// localhost:3306 /mydb
jdbc的主协议 子协议 ip地址以及端口号 要连接的数据库的名称
如果我们连接的是本地的数据库,那么localhost:3306可以省略
jdbc:mysql:///mydb