JDBC基础

导读:本篇文章讲解 JDBC基础,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

JDBC

1 介绍

java DataBase Connectify:java数据库连接技术,用于操作数据库。例如:mysql,Oracle,sql-Server。

JDBC实际上是一套用于连接数据库的接口规范。

2 JDBC常用接口

java…sql.*:所有JDBC接口都保存在这个包下面。

  • Connection:数据库连接
  • Statement:执行sql命令;
  • ResultSet:结果集;
  • DriverManager:获取Connection的工具。

3 使用JDBC的步骤

第一步:导入驱动包;

第二步:加载驱动;

第三步:获取数据库连接;

第四步:获取Statement对象,调用execute方法把SQL命令发送数据库执行;

数据库接收到发送过来的SQL命令后,会先检查SQL语法是否正确,如果正确编译SQL,最后执行SQL。

第五步:处理ResultSet结果集;

第六步:关闭资源;

4 举例

4.1 加载驱动

static {
		// 加载驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

4.2 单个删除

// 删除
	private static void testDelete(int studentId) {
		try (
			// 需要关闭的资源定义在这里
			// 获取数据库连接
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aa",  // 数据库URL
					"root",  // 用户名
					"root"); // 密码
			// 创建PreparedStatement对象
			PreparedStatement pstmt = conn.prepareStatement(
					"delete from student where stu_id = ?");
		) {
			// 设置sql的参数
			pstmt.setInt(1, 5);
			// 执行sql命令
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

4.3 多个删除:

// 批量删除
	private static void testDelete(int[] stuIds) {
		try (
			// 需要关闭的资源定义在这里
			// 获取数据库连接
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aa",  // 数据库URL
					"root",  // 用户名
					"root"); // 密码
			// 创建PreparedStatement对象
			PreparedStatement pstmt = conn.prepareStatement(
					"delete from student where stu_id = ?");
		) {
			for (int id : stuIds) {
				pstmt.setInt(1, id);
				// 把参数添加到PreparedStatement对象中
				pstmt.addBatch();
			}
			// 执行sql命令
			pstmt.executeBatch();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

4.4 修改:

// 修改
	private static void testUpdate(int studentId, String name) {
		try (
			// 需要关闭的资源定义在这里
			// 获取数据库连接
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aa",  // 数据库URL
					"root",  // 用户名
					"root"); // 密码
			// 创建PreparedStatement对象
			PreparedStatement pstmt = conn.prepareStatement(
					"update student set stu_name = ? where stu_id = ?");
		) {
			// 设置sql的参数
			pstmt.setString(1, name);
			pstmt.setInt(2, studentId);
			// 执行sql命令
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

4.5 查询:

// 查询
	private static void testQuery() {
		try (
			// 获取数据库连接
			Connection conn = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aa", 
					"root", 
					"root");
			// 创建PreparedStatement对象
			PreparedStatement pstmt = conn.prepareStatement(
					"select * from student");
			// 执行查询
			ResultSet rs = pstmt.executeQuery();
		) {
			// 遍历结果集
			while (rs.next()) { 
				int stuId = rs.getInt("stu_id");
				String stuName = rs.getString("stu_name");
				boolean gender = rs.getBoolean("gender");
				Date birthdate = rs.getDate("birthdate");
				String phone = rs.getString("phone");
				String hobby = rs.getString("hobby");
				System.out.println(stuId + ", " + stuName + ", "
					+ gender + "," + birthdate + "," + phone + "," + hobby);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81682.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!