当前位置:网站首页>【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();
点点关注点点赞支持一下博主吧~
边栏推荐
- Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
- Arduino tutorial - Simon games
- Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
- 漏了监控:Zabbix对Eureka instance状态监控
- ROS学习_基础
- 26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
- 接口自动化测试框架:Pytest+Allure+Excel
- Prefix and array series
猜你喜欢
The author is dead? AI is conquering mankind with art
树莓派3B更新vim
C language_ Double create, pre insert, post insert, traverse, delete
18. Multi level page table and fast table
微信公众号无限回调授权系统源码 全网首发
配置树莓派接入网络
Basic commands of MySQL
RichView TRVStyle 模板样式的设置与使用
Proteus -- Serial Communication parity flag mode
基于PyTorch和Fast RCNN快速实现目标识别
随机推荐
Thought map of data warehouse construction
Three methods of adding color to latex text
Raspberry pie serial port login and SSH login methods
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Configure raspberry pie access network
18.多级页表与快表
Applied stochastic process 01: basic concepts of stochastic process
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
Missing monitoring: ZABBIX monitors the status of Eureka instance
CDN acceleration and cracking anti-theft chain function
leetcode704. 二分查找(查找某个元素,简单,不同写法)
Pymongo gets a list of data
Simple use of MySQL database: add, delete, modify and query
“无聊猿” BAYC 的内忧与外患
UWA Pipeline 2.2.1 版本更新说明
leetcode6109. 知道秘密的人数(中等,周赛)
Top test sharing: if you want to change careers, you must consider these issues clearly!
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案