JDBC
在Java中 ,数据库存取技术客服呢为如下几类:
 1.JDBC 直接访问数据库
 2.JDO(java data object)技术
 3.第三方O/R工具 , 如Hibernate,Mybatis 等
JDBC是JAVA访问数据库的基石,JDO Hibernate MyBatis 等只是更好封装了JDBC
JDBC 是一个独立于特定数据库管理系统 通用的SQL数据库存取和操作的公共接口
 ,定义了用来访问数据库的标准java类库,使用这些类库可以以一种标准的方法方便地访问数据库资源。
获取连接
 方式一:
 1.导入mysql驱动 下载连接 添加链接描述
Driver  d = new com.mysql.cj.jdbc.Driver();
	//jdbc :  mysql协议
	//localhost: ip地址
	//3306 : 默认mysql的端口号
	//test :  要连接的数据库名
	String url ="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
	//将用户名密码封装在propertise中
	Properties info=new Properties(); 
	info.setProperty("user", "root");
	info.setProperty("password", "root");
	
 Connection  c= d.connect(url, info);
 
 System.out.println(c);
	
	
 
方式二: 对方式一的迭代
 1.获取Driver实现类对象 用反射来实现
 Class .forname
public class 方式二 {
	public static void main(String[] args) throws Exception {
		Class clazz= Class.forName("com.mysql.jdbc.Driver");
		
	Driver  driver=(Driver) clazz.newInstance();
	
	String url ="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
	//将用户名密码封装在propertise中
	Properties info=new Properties(); 
	info.setProperty("user", "root");
	info.setProperty("password", "root");
	
 Connection  c= driver.connect(url, info);
 
 System.out.println(c);
	}
}
  方式三    使用DriverManager替换Driver
  public class 方式三 {
  public static void main(String[] args) throws SQLException {
	  //1.获取driver实现类的对象
	  Driver  d = new com.mysql.cj.jdbc.Driver();
	//注册驱动
	
	  //获取连接
		String url ="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
		String user="root";
		String password="root";
	  Connection connection = DriverManager.getConnection(url,user,password);
	  System.out.println(connection);
}
}
  
 
```java
方式四   相较于方式三更加省略 可以只是加载驱动,不用显示注册驱动过了
 public class 方式四{
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
	  //1.获取driver实现类的对象
	     Class.forName("com.mysql.cj.jdbc.Driver");
	//注册驱动
	  //获取连接
		String url ="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC";
		String user="root";
		String password="root";
	  Connection connection = DriverManager.getConnection(url,user,password);
	  System.out.println(connection);
}
}
 
最终版 将 数据库连接需要的四个基本信息声明在配置文件中,通过夺取配置文件的方式,获取连接
放在src目录下的配置文件
 
public class finaly {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
	//1.读取配置文件中的四个基本信息
	InputStream is = finaly.class.getClassLoader().getResourceAsStream("jdbc.properties");
	Properties p =new Properties();
	p.load(is);
	String user = p.getProperty("user");
	String password = p.getProperty("password");
	String url = p.getProperty("url");
	String driver = p.getProperty("driver");
	Class.forName(driver);
	Connection connection = DriverManager.getConnection(url, user, password);
	  System.out.println(connection);
}
}
 
好处 :1.实现了数据和代码的分离。
 2.如果需要修改配置文件信息,可以避免程序重新打包。










