基本介绍
- JDBC为访问不同的数据库提供了统一的接口,蔽了细节问题,降低了学习成本。
- java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作。
(相当于为java厂商指定了规范,规定了一套接口规范,让不同的数据库厂商实现,在java程序中统一调用接口的方法即可)
JDBC API
- JDBC API是一系列的接口,它统一和规范了应用程序与数据库的连接,执行sql语句,得到返回值结果等各类操作,相关类和接口在java.sql与javax.sql包中。
JDBC编写步骤
- 注册驱动 加载Driver类
- 获取连接 得到Connection
- 执行增删改查 发送sql给mysql执行
- 释放资源 关闭相关连接
连接数据库的五种方式
import com.mysql.cj.jdbc.Driver;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcConn {
@Test
public void connect01() throws SQLException {
Driver driver = new Driver();
String url = "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","1");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
@Test
public void connect02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","1");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
@Test
public void connect03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Class<?> aClass = Class.forName("com.mysql.cj.jdbc.Driver");
Driver driver = (Driver) aClass.newInstance();
String url = "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String user ="root";
String password = "1";
DriverManager.registerDriver(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
@Test
public void connect04() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
String user ="root";
String password = "1";
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
@Test
public void connect05() throws SQLException, IOException, ClassNotFoundException {
Properties properties = new Properties();
properties.load(new FileInputStream("D:\ideaworkspaces\JDBCStudy\src\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection);
}
}