一.数据库编程的必备条件
编程语言:java c c++ Python
数据库 Oracle,MySQL,SQL Server
数据库驱动包:不同的数据库,对应不同的编程语言提供了不同的数据库驱动包:MySQL提供了Java的驱动包
mysql=connector-java,需要就Java操作MySQL需要该驱动包
二.Java的数据库编程
JDBC,即Java Database Connectivity,java数据库连接。是一种用于执行SQL语句的Java API,它是
Java中的数据库连接规范。这个API由 java.sql.,javax.sql. 包中的一些类和接口组成,它为Java
开发人员操作数据库提供了一个标准的API,可以为多种关系数据库提供统一访问。
![](https://img-blog.csdnimg.cn/img_convert/c60dfe0a4bd2cbd922918acc8e59abfe.png)
三.jdbc工作原理
JDBC 为多种关系数据库提供了统一访问方式,作为特定厂商数据库访问API的一种高级抽象,它主要包
含一些通用的接口类。
![](https://img-blog.csdnimg.cn/img_convert/f44ac180fead6c3e7793cf8be18fcc90.png)
JDBC优势:
Java语言访问数据库操作完全面向抽象接口编程
开发数据库应用不用限定在特定数据库厂商的API
程序的可移植性大大增强
MySQL是一个-服务器结构的程序
![](https://img-blog.csdnimg.cn/img_convert/8a32a173e0ef10d0ca065dcd1bf35181.png)
![](https://img-blog.csdnimg.cn/img_convert/83a3ae99a2a0336452ecb0cfa3891e8d.png)
![](https://img-blog.csdnimg.cn/img_convert/2553e69ca5cf4f25f708bc6d39e3dc16.png)
![](https://img-blog.csdnimg.cn/img_convert/9e730feb7e9a0495d21159923b1753c4.png)
![](https://img-blog.csdnimg.cn/img_convert/9eabd9a5692b7a53e42cdf9aec75ceda.png)
![](https://img-blog.csdnimg.cn/img_convert/0eb5f8626e707ca5bc5ad93d0409e403.png)
四.JDBC编程
1.创建项目
![](https://img-blog.csdnimg.cn/img_convert/264fa957acc64fb8236b0ab5fa7212d2.png)
2.引入依赖
JDBC编程需要用到MySQL的驱动包
驱动包就是将mysql本身的API给转化成jdbc风格的
1)下载驱动包
![](https://img-blog.csdnimg.cn/img_convert/d1f248b3f2ee05770c8b12ee8b357fbe.png)
去maven中央仓库去下载
![](https://img-blog.csdnimg.cn/img_convert/e54430bfdc4aaae07cd022d861efc68b.png)
https://mvnrepository.com/
![](https://img-blog.csdnimg.cn/img_convert/37a02c3b2897fe0332365407bfdfc46d.png)
![](https://img-blog.csdnimg.cn/img_convert/b518db0880fb8ca404a5c211fca410ff.png)
![](https://img-blog.csdnimg.cn/img_convert/3b530db45265d3a425c258d81a97e791.png)
2)导入到项目中
a)创建个目录,随便取名字
![](https://img-blog.csdnimg.cn/img_convert/f73caaa9d5986a8c29bab62013d2f366.png)
b)把刚才下载的jar文件拷贝到刚才的目录
![](https://img-blog.csdnimg.cn/img_convert/b0a885458a6608d2d39d5a6ba79334f4.png)
![](https://img-blog.csdnimg.cn/img_convert/80b217bc70fcd5863af75bfca74af7e5.png)
c)右键刚刚的目录,有一个选项,佳作add as library
![](https://img-blog.csdnimg.cn/img_convert/a6348364faa3453052e24b8b3b680f3e.png)
![](https://img-blog.csdnimg.cn/img_convert/b3476fac8150f0b66d511e2556df024e.png)
3)编写代码
1.创建DataSource对象,这个对象就描述了数据库服务器在哪里
![](https://img-blog.csdnimg.cn/img_convert/1c3b2db76255c095258af5739a35638e.png)
![](https://img-blog.csdnimg.cn/img_convert/14ecfd954990334917a8556f671277c3.png)
父类引用指向子类实例
![](https://img-blog.csdnimg.cn/img_convert/b345174ddfe74e1c6f41c6328b122862.png)
![](https://img-blog.csdnimg.cn/img_convert/42862ec21071139361c44795f52a2bb5.png)
![](https://img-blog.csdnimg.cn/img_convert/23c5edc340377ede1a257105fab67fb4.png)
URL:Uniform Resource Locator
唯一资源定位符
![](https://img-blog.csdnimg.cn/img_convert/a91bc6f074e884045cdcd26b87d9eead.png)
![](https://img-blog.csdnimg.cn/img_convert/f0d1513407a6d227c10da9c6974915f2.png)
![](https://img-blog.csdnimg.cn/img_convert/2274e94f7169ed8e4e473da6f1016cd1.png)
![](https://img-blog.csdnimg.cn/img_convert/c8c3027b9a16ec8cc3daac34291b39af.png)
![](https://img-blog.csdnimg.cn/img_convert/7dc81fe41b06c1cb829791b541ed25fd.png)
![](https://img-blog.csdnimg.cn/img_convert/b95e28b627014c7c6ceb4ddc70728e36.png)
2.建立连接
![](https://img-blog.csdnimg.cn/img_convert/77fd48c889c9567837a0068dce605f53.png)
![](https://img-blog.csdnimg.cn/img_convert/2d7b9127e2b1234087417825e81643ee.png)
![](https://img-blog.csdnimg.cn/img_convert/f9e485f803328f63669c93b6c3fd5e8b.png)
3操作数据库(插入)-构造语句
![](https://img-blog.csdnimg.cn/img_convert/e9361f698a0e2593f55d19e070f50b46.png)
![](https://img-blog.csdnimg.cn/img_convert/ce16850d4ae08556ddf72b5e75cb5249.png)
![](https://img-blog.csdnimg.cn/img_convert/88acc0187d9e6da43e8466d6c0edbe6b.png)
4.执行sql
![](https://img-blog.csdnimg.cn/img_convert/36d4b0ca2f0078dc7d3f36e8f3996548.png)
![](https://img-blog.csdnimg.cn/img_convert/dcbf87a9a51e0a06f17b9713f915c191.png)
5.释放资源
![](https://img-blog.csdnimg.cn/img_convert/b95d1484a4cbde3fa2d7f2f4e8066436.png)
释放资源的时候先释放statement,再释放connection
先创建的后释放
![](https://img-blog.csdnimg.cn/img_convert/c7116bc0dad8ccce6f8fef1099e88abb.png)
4)总结
![](https://img-blog.csdnimg.cn/img_convert/0ac638f93eba4bb954a7d99e90d165b9.png)
(42条消息) 如何查看MySQL的端口号?_安西宁的博客-CSDN博客_mysql8 查看端口
五.灵活处理插入删除数据
0.以前的方法
![](https://img-blog.csdnimg.cn/img_convert/3a1da19742cb0a7750730e77a5182768.png)
1.使用户输入
![](https://img-blog.csdnimg.cn/img_convert/b57a36332ddce7daa6c2b6f417c89393.png)
2.将id和name替换到insert语句对应的位置上
![](https://img-blog.csdnimg.cn/img_convert/f5c22d2bdbd9bfa74ea6222fc5bf0db9.png)
方法1,用字符串的拼接
![](https://img-blog.csdnimg.cn/img_convert/f7fc782393fbca69886089c20f8f60e8.png)
方法2:替代写法
![](https://img-blog.csdnimg.cn/img_convert/144d1038a720439c87a94618a0fe303d.png)
3.进行替换操作
![](https://img-blog.csdnimg.cn/img_convert/2a2e2c483df98ca330ccafc969cb2426.png)
![](https://img-blog.csdnimg.cn/img_convert/a7e7852d57d1476c3bb5f72f0469370f.png)
4.打印拼接好的sql
![](https://img-blog.csdnimg.cn/img_convert/e33303a42e0ed03f6c1d83d1219059f5.png)
![](https://img-blog.csdnimg.cn/img_convert/f030db637dc81c1c34fb4438b202192d.png)
六.注意事项
1.无法用idea连接
![](https://img-blog.csdnimg.cn/img_convert/6ec252518ca0b95ce8dbfa82ad3e5712.png)
![](https://img-blog.csdnimg.cn/img_convert/40105d2fde2a29583e709747b8b22ccb.png)
![](https://img-blog.csdnimg.cn/img_convert/4924618f9d00464695686da62fde58b9.png)
2.连接池
![](https://img-blog.csdnimg.cn/img_convert/29d3e1eb313e44a10e2ae4e8e4e7e044.png)
![](https://img-blog.csdnimg.cn/img_convert/0525d770c112aa827f7adc00ba3fc3e8.png)
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class JDBC {
public static void main(String[] args) throws SQLException {
Scanner scanner = new Scanner(System.in);
// 1. 创建好数据源
DataSource dataSource = new MysqlDataSource();
// 设置数据库所在的地址
((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
// 设置登录数据库的用户名
((MysqlDataSource) dataSource).setUser("root");
// 这个是设置登录数据库的密码
((MysqlDataSource) dataSource).setPassword("2222");
// 2. 让代码和数据库服务器建立连接~~ 相当于到达了菜鸟驿站
Connection connection = dataSource.getConnection();
// 2.5 让用户通过控制台输入一下待插入的数据.
System.out.println("请输入学号: ");
int id = scanner.nextInt();
System.out.println("请输入姓名: ");
String name = scanner.next();
// 3. 操作数据库. 以插入数据为例.
// 关键所在就是构造一个 SQL 语句~
// 在 JDBC 中构造的 SQL, 不必带上 ;
// ; 只是在命令行中用来区分不同的语句. 现在是直接在代码中操作~~
String sql = "insert into student values(?, ?)";
// 此处光是一个 String 类型的 sql 还不行, 需要把这个 String 包装成一个 "语句对象"
PreparedStatement statement = connection.prepareStatement(sql);
// 进行替换操作.
statement.setInt(1, id);
statement.setString(2, name);
System.out.println("statement: " + statement);
// 4. 执行 SQL , 相当于扫码取件
// SQL 里面如果是 insert, update, delete, 都使用 executeUpdate 方法.
// SQL 里面如果是 select, 则使用 executeQuery 方法.
// 返回值就表示这个操作, 影响到了 几行. 就相当于在控制台里输入 sql 之后, 得到的数字~
int ret = statement.executeUpdate();
System.out.println(ret);
// 5. 此时 SQL 已经执行完毕. 然后还需要释放资源.
statement.close();
connection.close();
}
}
七 .查找操作
![](https://img-blog.csdnimg.cn/img_convert/e891ad7a67897a1eeb67ba21b714e2ee.png)
![](https://img-blog.csdnimg.cn/img_convert/b1b41d17db41dfd91a946a824565ba7a.png)
![](https://img-blog.csdnimg.cn/img_convert/047bb3e5d2d214b63c88487c0c2dd005.png)
![](https://img-blog.csdnimg.cn/img_convert/4d7b607d506f4be7ce4faa43629441fa.png)
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCSelect {
public static void main(String[] args) throws SQLException {
// 查找数据库中的记录
// 这个查找和前面的操作就不太一样了~~ 多了一个步骤, 要遍历结果集合~
// 1. 创建数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("2222");
// 2. 和数据库服务器建立连接
Connection connection = dataSource.getConnection();
// 3. (此处不输入条件了, 直接查找所有记录)
// 4. 构造 SQL
String sql = "select * from student";
PreparedStatement statement = connection.prepareStatement(sql);
// 5. 执行 SQL.
// 此处要使用的是 executeQuery.
// executeUpdate 只能返回一个 int.
// executeQuery 返回的是一个 ResultSet 对象. 可以把这个对象视为是一个 "临时表"
ResultSet resultSet = statement.executeQuery();
// 6. 遍历临时表, 拿到里面的数据.
// resultSet 简单的当成一个类似于 "迭代器" 这样的东西来看待~
// next 如果没有到达末尾, 就是返回 true, 要继续循环.
// next 如果到达末尾, 就返回 false, 结束循环.
while (resultSet.next()) {
// 在这里就可以取这一行的数据了.
// 通过 ResultSet 里面的 getXXX 方法, 来获取到这里指定的列.
// 取 id, id 是整数, getInt
int id = resultSet.getInt("id");
// 取 name, name 是 String, getString
String name = resultSet.getString("name");
System.out.println(id + ": " + name);
}
// 7. 关闭连接, 释放资源
resultSet.close();
statement.close();
connection.close();
}
}
八.workbench
![](https://img-blog.csdnimg.cn/img_convert/ef9efe107ddab5ac58fc239ea4d54c50.png)
![](https://img-blog.csdnimg.cn/img_convert/6258990e2a74d86274e864858d43215c.png)
![](https://img-blog.csdnimg.cn/img_convert/3188a9ee8b702c7cc2140f30e7c9fbb8.png)
![](https://img-blog.csdnimg.cn/img_convert/dd171cbf2ddcee8221714ab53cefdf74.png)