1、JDBC介绍
JDBC(Java Database Connectivity)是一个独立于特定数据库管理系统、通用的SQL数据库存取和操作的公共接口(一组API),定义了用来访问数据库的标准Java类库,(java.sql,javax.sql)使用这些类库可以以一种标准的方法、方便地访问数据库资源。
JDBC为访问不同的数据库提供了一种统一的途径,为开发者屏蔽了一些细节问题。
JDBC的目标是使Java程序员使用JDBC可以连接任何提供了JDBC驱动程序的数据库系统,这样就使得程序员无需对特定的数据库系统的特点有过多的了解,从而大大简化和加快了开发过程。
2、使用JDBC连接数据库的方式
方式一:
通过显示使用API连接MySQL5.6
public class M1 {
public static void main(String[] args) {
try {
//1.提供java.sql.Driver接口实现类的对象
Driver driver = null;
driver = new com.mysql.jdbc.Driver();
//2.提供url,指明具体操作的数据
String url = "jdbc:mysql://localhost:3306/对应数据库名";
//3.提供Properties的对象,指明用户名和密码
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "你的密码");
//4.调用driver的connect(),获取连接
Connection conn = driver.connect(url, info);
System.out.println(conn);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
复制代码
方式二:
说明:相较于方式一,这里使用反射实例化Driver,不在代码中体现第三方数据库的API。
public class M2 {
public static void main(String[] args) {
try {
//1.实例化Driver
String className = "com.mysql.cj.jdbc.Driver";
Class clazz = Class.forName(className);
Driver driver = (Driver) clazz.newInstance();
//2.提供url,指明具体操作的数据
String url = "jdbc:mysql://localhost:3306/对应数据库名?useSSL=false&serverTimezone=UTC";
//3.提供Properties的对象,指明用户名和密码
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "你的密码");
//4.调用driver的connect(),获取连接
Connection conn = driver.connect(url, info);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
复制代码
方式三:
说明:使用DriverManager实现数据库的连接。体会获取连接必要的4个基本要素。
public class M3 {
public static void main(String[] args) {
try {
//1.数据库连接的4个基本要素:
String url = "jdbc:mysql://localhost:3306/对应数据库名?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "你的密码";
String driverName = "com.mysql.cj.jdbc.Driver";
//2.实例化Driver
Class clazz = Class.forName(driverName);
Driver driver = (Driver) clazz.newInstance();
//3.注册驱动
DriverManager.registerDriver(driver);
//4.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
复制代码
方法四:
说明:不必显式的注册驱动了。因为在DriverManager的源码中已经存在静态代码块,实现了驱动的注册。
public class M4 {
public static void main(String[] args) {
try {
//1.数据库连接的4个基本要素:
String url = "jdbc:mysql://localhost:3306/对应数据库名?useSSL=false&serverTimezone=UTC";
String user = "root";
String password = "你的密码";
String driverName = "com.mysql.cj.jdbc.Driver";
//2.加载驱动 (①实例化Driver ②注册驱动)
Class.forName(driverName);
//Driver driver = (Driver) clazz.newInstance();
//3.注册驱动
//DriverManager.registerDriver(driver);
/*
可以注释掉上述代码的原因,是因为在mysql的Driver类中声明有:
static {
try {
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
*/
//3.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
复制代码
方法五:
说明:使用配置文件的方式保存配置信息,在代码中加载配置文件
使用配置文件的好处:
①实现了代码和数据的分离,如果需要修改配置信息,直接在配置文件中修改,不需要深入代码
②如果修改了
配置信息,省去重新编译的过程。
在项目的src目录下创建jdbc.properties
user=root
password=你的密码
url=jdbc:mysql://localhost:3306/对应数据库名?useSSL=false&serverTimezone=UTC
driverClass=com.mysql.cj.jdbc.Driver
复制代码
public class M5 {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
//1.加载配置文件,使用IO流读取文件
InputStream is = M5.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
//2.读取配置信息
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
//3.加载驱动
Class.forName(driverClass);
//4.获取连接
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END