当前位置:网站首页>【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();
点点关注点点赞支持一下博主吧~
边栏推荐
- UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
- GET 和 POST 请求类型的区别
- 树莓派串口登录与SSH登录方法
- What is the difference between int (1) and int (10)? Senior developers can't tell!
- PCL realizes frame selection and clipping point cloud
- Prefix and array series
- MPLS experiment
- C语言_双创建、前插,尾插,遍历,删除
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
- 作者已死?AI正用艺术征服人类
猜你喜欢

Fedora/rehl installation semanage

Configure raspberry pie access network

Windows Server 2016 standard installing Oracle

AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm

OpenGL ES 学习初识(1)

Top test sharing: if you want to change careers, you must consider these issues clearly!

The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
![[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5](/img/c3/7a147151b7338cf38ffbea24e8bafd.jpg)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5

Basic commands of MySQL

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
随机推荐
Configure raspberry pie access network
软件测试外包到底要不要去?三年真实外包感受告诉你
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
OpenGL ES 学习初识(1)
前缀和数组系列
pymongo获取一列数据
Raspberry pie serial port login and SSH login methods
Establishment and operation of cloud platform open source project environment
[daily question] 729 My schedule I
[some special grammars about C]
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
呆错图床系统源码图片CDN加速与破解防盗链功能
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
Internal and external troubles of "boring ape" bayc
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
雲上有AI,讓地球科學研究更省力