JDBC

—— java database connectivity Java数据库连接。
实际上JDBC是JAVA中一套和数据库交汇的API
API(application program interface 应用程序编程接口)

——为什么使用JDBC:由于需要连接不同类型数据库(oracle,mysql等等),为了避免java每一种数据库都需要学习一套新的连接方法,sun公司提出了一个jdbc接口,各个数据库厂商根据此接口写实现类(数据库驱动) ,这样java程序员只需要学习jdbc接口中方法的调用,就可以连接任意数据库,而且代码完全遵循JDBC标准写的话将来换了数据库,代码也不需要改变

######如何使用JDBC
1.创建maven工程
2.下载mysql驱动jar包
——如何下载?
外网登录:maven.aliyun.com
找到mysql对应的包地址,复制xml到工程的pom.xml中
在这里插入图片描述

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>yao.com</groupId>
	<artifactId>JDBC01</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.9-rc</version>
</dependency>

	</dependencies>
</project>

如何在eclipse中创建并使用

  1. 注册驱动
  2. 获取连接对象
  3. 创建SQL执行对象
  4. 执行SQL语句
  5. 关闭资源
package SQLJDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DEMO01 {
	public static void main(String[] args) throws Exception {
		//1.注册驱动
			Class.forName("com.mysql.cj.jdbc.Driver");
		//2.获取连接对象
			Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true", "yaojiahui", "741236");
			System.out.println(connection);
		//3.创建sql执行对象
			Statement statement=connection.createStatement();
		//4.执行sql语句
			String sql="create table jdbcdt1(id int,name varchar(10))";
			statement.execute(sql);
		//5.关闭资源
			connection.close();
	}

}

执行SQL语句的方法

  • execute:可以执行任何SQL语句,但是推荐执行create/alter/drop
  • executeUpdate:执行增删改的操作的SQL
  • executeQuery:执行查询操作的SQL

插入

@Test
	public void test01() throws Exception {
		System.out.println("test01 测试执行插入语句");
		//反射,注册驱动
		Class.forName("com.mysql.cj.jdbc.Driver");
		//获取连接对象
		Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true", "yaojiahui", "741236");
		//创建SQL执行对象
		Statement statement=connection.createStatement();
		//创建SQL语句并执行
		String sql="insert into jdbcdt1 values(1,'test01')";
		//此方法会返回一个返回值,返回值为生效的行数
		int row=statement.executeUpdate(sql);
		System.out.println("over:"+row);
		//关闭资源
		connection.close();
	}

修改

@Test
	public void test02() {
		try {
			System.out.println("test02 测试执行修改语句");
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true", "yaojiahui", "741236");
			Statement statement = connection.createStatement();
			String sql="update jdbcdt1 set name = 'test02' where id=1";
			int row = statement.executeUpdate(sql);
			System.out.println("over:"+row);
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

删除

@Test
	public void test03() {
		try {
			System.out.println("test03 测试执行删除语句");
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true", "yaojiahui", "741236");
			Statement statement = connection.createStatement();
			String sql = "delete from jdbcdt1 where id=1";
			int row=statement.executeUpdate(sql);
			System.out.println("over:"+row);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

查询

	@Test
	public void test04() {
		try {
			System.out.println("test04 测试查询语句");
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true", "yaojiahui", "741236");
			Statement statement = connection.createStatement();
			String sql = "select * from emp";
			//executeQuery方法会返回一个返回集
			ResultSet rt = statement.executeQuery(sql);
			System.out.println(rt);
			//遍历返回的集中的数据
			for(;rt.next();) {
				//通过字段名获取数据
				String name = rt.getString("name");
				//也可通过字段位置来获取数据,若上方的查询条件是*,则getString(2)表示的就是第二个字段名
				//String name = rt.getString(2);
				int comm = rt.getInt("comm");
				int mgr = rt.getInt("mgr");
				System.out.println("name: "+name+"\t"+"comm: "+comm+" "+"\t"+"mgr: "+mgr);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

在这里,我自己加了一些数据进表里,
在这里插入图片描述
test04方法的运行结果为
在这里插入图片描述

关于MySQL8获取连接对象时的问题

Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb2?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true", "yaojiahui", "741236");

新版本的MySQL在创建注册驱动的时候如果还是写老版本的com.mysql.jdbc.Driver时,会报一个Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.的异常
意思是加载类“ com.mysql.jdbc.Driver”。 不推荐使用。 新的驱动程序类为“ com.mysql.cj.jdbc.Driver”。 通过SPI自动注册驱动程序,通常不需要手动加载驱动程序类。
原因似乎是新版本的MySQL改写了注册驱动

另外新版本不再支持直接写jdbc:mysql://localhost:3306/数据库名这个写法
需要后面加
?useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐