一、什么是JDBC编程
💡 JDBC是Java DataBase Connectivity的缩写,它是Java程序访问数据库的标准接口.其提供了一些类和方法,可供程序猿对任何数据库的日常开发使用.
![](https://img-blog.csdnimg.cn/img_convert/005c11274e205fcf6423a6d568f2bdec.jpeg#clientId=uba3f7181-3d97-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=uce122610&margin=[object%20Object]&name=33.jpg&originHeight=328&originWidth=377&originalType=binary&ratio=1&rotation=0&showTitle=false&size=15557&status=done&style=none&taskId=ubfd14eb8-44ce-497c-a78a-787773ed460&title=)
二、驱动包的安装与下载
(1)下载驱动包
方法一:去oracle官网下载(不推荐,因为要注册麻烦).
方法二:去maven中央仓库(https://mvnrepository.com/).
![](https://img-blog.csdnimg.cn/img_convert/842949a9f0196ae189559c2295d05547.png#clientId=uba3f7181-3d97-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=476&id=u1fc37786&margin=[object%20Object]&name=image.png&originHeight=536&originWidth=1008&originalType=binary&ratio=1&rotation=0&showTitle=false&size=127359&status=done&style=none&taskId=u04b34e4e-4dd4-4047-a2e8-ab833ab36c5&title=&width=896)
![](https://img-blog.csdnimg.cn/img_convert/89c4f209f294c012764366871c00feb5.png#clientId=uba3f7181-3d97-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=586&id=u914c6342&margin=[object%20Object]&name=image.png&originHeight=659&originWidth=976&originalType=binary&ratio=1&rotation=0&showTitle=false&size=124251&status=done&style=none&taskId=u4c4cdbd3-84b3-4fe8-87fb-e1124d7fd0c&title=&width=867.5555555555555)
![](https://img-blog.csdnimg.cn/img_convert/a5fc4a090c007dcef39599c6f57d183d.png#clientId=uba3f7181-3d97-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=164&id=u111c1790&margin=[object%20Object]&name=image.png&originHeight=185&originWidth=522&originalType=binary&ratio=1&rotation=0&showTitle=false&size=25129&status=done&style=none&taskId=uff8c8b30-f805-4ff4-a7bf-b4d82c137f1&title=&width=464)
![](https://img-blog.csdnimg.cn/img_convert/2a29b7dfb02607e0dbf41c9fec7d626a.png#clientId=uba3f7181-3d97-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=862&id=u1c778ac1&margin=[object%20Object]&name=image.png&originHeight=970&originWidth=787&originalType=binary&ratio=1&rotation=0&showTitle=false&size=166889&status=done&style=none&taskId=u3acd1c3a-dd4f-43ae-8217-25c131a37b6&title=&width=699.5555555555555)
三、IDE中使用数据库
(1)创建数据源
DataSource dataSource = new MysqlDataSource();
((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/test1?characterEncoding=utf8&useSSL=false");
((MysqlDataSource)dataSource).setUser("root");
((MysqlDataSource)dataSource).setPassword("127001");
URL里面参数的含义:
![](https://img-blog.csdnimg.cn/img_convert/9855427499d6738c9e2bef455ba99440.png#clientId=uba3f7181-3d97-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=372&id=pe2nM&margin=[object%20Object]&name=image.png&originHeight=419&originWidth=1134&originalType=binary&ratio=1&rotation=0&showTitle=false&size=23347&status=done&style=none&taskId=u8fb9a9eb-a05d-41b9-869b-61894e81a46&title=&width=1008)
Connection connection = dataSource.getConnection();
(3)输入
System.out.println("请输入修改的id ");
int id = scanner.nextInt();
System.out.println("请输入修改的name ");
String name = scanner.next();
运行:
![](https://img-blog.csdnimg.cn/img_convert/ae69c1f9560f193d564ce47e94da8819.png#clientId=ue27956b1-5396-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=146&id=u63f92e04&margin=[object%20Object]&name=image.png&originHeight=164&originWidth=805&originalType=binary&ratio=1&rotation=0&showTitle=false&size=10556&status=done&style=none&taskId=ud24aca70-6b4c-47f6-b8e5-44d1785dfdc&title=&width=715.5555555555555)
(4)打包创建对象
String sql = "update student set name = ? where id = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1,name);
statement.setInt(2,id);
System.out.println(statement);
打印日志:
![](https://img-blog.csdnimg.cn/img_convert/86823f1e4116f243570da5456cc38612.png#clientId=ue27956b1-5396-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=43&id=u6ff58475&margin=[object%20Object]&name=image.png&originHeight=48&originWidth=1351&originalType=binary&ratio=1&rotation=0&showTitle=false&size=7701&status=done&style=none&taskId=ub4a8a0bf-9ad4-4261-a09b-44fb571683a&title=&width=1200.888888888889)
![](https://img-blog.csdnimg.cn/img_convert/bf4f0316986f3d15ad53431fe84674e3.jpeg#clientId=ue27956b1-5396-4&crop=0&crop=0&crop=1&crop=1&from=ui&id=pUmR5&margin=[object%20Object]&name=29.jpg&originHeight=408&originWidth=550&originalType=binary&ratio=1&rotation=0&showTitle=false&size=19926&status=done&style=none&taskId=ucdc48a81-076f-44e3-aa8d-fbf36fd4834&title=)
(5)执行sql语句
int ret = statement.executeUpdate();
System.out.println(ret);
(6)释放
原则: 谁后创建,谁就先释放,就像开冰箱拉柜子顺序一样.
statement.close();
connection.close();
(7)结果:
![](https://img-blog.csdnimg.cn/837b246dd8a74b559b7a48f9e80610cc.png)
![](https://img-blog.csdnimg.cn/a140030b0f124fa1bde0876ef5f0025b.png)