当前位置:网站首页>【JDBC】快速入门教程
【JDBC】快速入门教程
2022-07-06 07:05:00 【漩涡编程,一鸣惊人】
1.什么是JDBC?
JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据。JDBC代表Java数据库连接。
JDBC库中所包含的API通常与数据库使用于:
- 连接到数据库
- 创建SQL或MySQL语句
- 在数据库中执行SQL或MySQL查询
- 查看和修改数据库中的数据记录
2.JDBC环境配置
- JAVA(JDK)安装
- 数据库系统的安装(如MySQL,SQL Server的安装)
3.创建JDBC应用程序
建立一个JDBC应用程序,本教程以java连接SQL Server为例,分六个步骤进行:
1.导入包
在程序中包含数据库编程所需的JDBC类。大多数情况下,使用 import java.sql.*就足够了
2.注册JDBC驱动程序
初始化驱动程序,打开与数据库的通信。
Class.forName("com.mysql.cj.jdbc.Driver");
3.打开链接
使用DriverManager.getConnection() 方法来创建一个Connection对象,
String user="sa";
String pwd="123456";
System.out.println("连接数据库中...");
Connection con=DriverManager.getConnection(url,user,pwd);
4.执行一个查询
使用一个Statement类型或PreparedStatement的对象,并提交一个SQL语句到数据库执行查询:
System.out.println("创建statement...");
Statement sm=con.createStatement();
String sql="SELECT * FROM Students";
ResultSet rs=sm.executeQuery(sql);
如果要执行一个SQL语句:UPDATE,INSERT,DELETE语句,如下:
sm=con.createStatement();
String sql="DELETE FROM Students";
ResultSet rs=sm.executeUpdate(sql);
5. 从结果集中提取数据
这一步中演示如何从数据库中获取查询结果的数据。可以使用适当的ResultSet.getXXX()方法来检索的数据结果如下:
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
6. 清理环境资源
在使用JDBC与数据交互操作数据库中的数据后,应该明确地关闭所有的数据库资源以减少资源的浪费,对依赖于JVM的垃圾收集如下:
//STEP 6: Clean-up environment
rs.close();
stmt.close();
con.close();
点点关注点点赞支持一下博主吧~
边栏推荐
- 【Hot100】739. 每日溫度
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- UWA Pipeline 2.2.1 版本更新说明
- Pallet management in SAP SD delivery process
- GET 和 POST 请求类型的区别
- 接口自动化测试框架:Pytest+Allure+Excel
- Visitor tweets about how you can layout the metauniverse
- Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
- 指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
- Simple use of JWT
猜你喜欢

Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)

Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin

Visitor tweets about how you can layout the metauniverse

Kubernetes cluster builds ZABBIX monitoring platform

开源的网易云音乐API项目都是怎么实现的?

数据仓库建设思维导图

前缀和数组系列

How to reconstruct the class explosion caused by m*n strategies?

Setting and using richview trvstyle template style

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作
随机推荐
BIO模型实现多人聊天
Setting and using richview trvstyle template style
Librosa audio processing tutorial
漏了监控:Zabbix对Eureka instance状态监控
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Hydra common commands
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
Fedora/rehl installation semanage
Embed UE4 program into QT interface display
Visitor tweets about how you can layout the metauniverse
Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
leetcode841. 钥匙和房间(中等)
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
Every API has its foundation when a building rises from the ground
What does UDP attack mean? UDP attack prevention measures
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
UniPro甘特图“初体验”:关注细节背后的多场景探索
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
leetcode59. 螺旋矩阵 II(中等)