# jdbc **Repository Path**: hrbu-2022/jdbc ## Basic Information - **Project Name**: jdbc - **Description**: 练习jdbc项目 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-11-08 - **Last Updated**: 2025-11-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 准备驱动 到maven的网站搜素 [https://mvnrepository.com/](https://mvnrepository.com/) [https://mvnrepository.com/artifact/com.mysql/mysql-connector-j/8.3.0](https://mvnrepository.com/artifact/com.mysql/mysql-connector-j/8.3.0) ![image-20241108085321274](imgs/image-20241108085321274.png) # 添加到IDEA项目中,并作为一个库存在(类似于JDK) - 新建目录libs ![image-20241108085610029](imgs/image-20241108085610029.png) - 复制jar包到 libs目录 - 设置为库 ![image-20241108085656382](imgs/image-20241108085656382.png) ![image-20241108085718586](imgs/image-20241108085718586.png) # 编写第一个jdbc程序 ```java package com.neuedu.jdbc; import java.sql.*; /** * @author 金山 * 项目:jdbc * site: https://blog.fulfill.com.cn * 描述 入门 * @data 2024/11/88:48 */ public class JDBCTest { /** * 旧版本 8- * // Class.forName("com.mysql.jdbc.Driver"); * @param args */ public static void main(String[] args) { Connection con = null; Statement stmt = null; ResultSet rs = null; try { //载入JDBC驱动程序: //8.0+ Class.forName("com.mysql.cj.jdbc.Driver"); //建立数据库连接对象: // String url = "http://www.baidu.com:80/search"; // String url = "jdbc:mysql//localhost:3306/test"; String url = "jdbc:mysql://127.0.0.1:3306/test"; String username = "root"; String password = "root"; con = DriverManager.getConnection(url,username,password); //创建Statement对象 stmt = con.createStatement(); /** * CRUD 增删改查 * query : select * update : insert update delete */ String sql = " select count(1) c from emp "; rs = stmt.executeQuery(sql); //解析结果集 if(rs.next()){ //进入到 if 中说明有数据 //rs 现在指向的是数据 行 // int count = rs.getInt(1); int count = rs.getInt("c"); System.out.println("count = " + count); } } catch (ClassNotFoundException | SQLException e) { throw new RuntimeException(e); }finally { //关闭资源 if(rs != null){ try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if(stmt != null){ try { stmt.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if(con != null){ try { con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } } } ``` # 事务操作 ```sql -- 创建账户表 CREATE TABLE account( id INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(1000) NOT NULL, amount DECIMAL(8,2) ); -- 准备数据 INSERT INTO account(NAME,amount) VALUES ('张三',2000), ('李四',2000); SELECT * FROM account; -- 转账 张三给李四转账 500 -- 给张三 减去 500 UPDATE account SET amount = amount - 500 WHERE id = 1; -- 李四 加上 500 UPDATE account SET amount = amount + 500 WHERE id = 2; ``` # 单元测试 使用JUnit - 下载 https://mvnrepository.com/artifact/junit/junit/4.13.2 - https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar 赋值 jar 到 libs中, 使用 Libs ![image-20241108134210217](imgs/image-20241108134210217.png)