当前位置:网站首页>[JDBC] quick start tutorial
[JDBC] quick start tutorial
2022-07-06 07:09:00 【Vortex programming, a blockbuster】
1. What is? JDBC?
JDBC API It's a Java API, You can access any type of table column data , In particular, data stored in relational databases .JDBC representative Java Database connection .
JDBC The library contains API Usually used with databases :
- Connect to database
- establish SQL or MySQL sentence
- Execute... In the database SQL or MySQL Inquire about
- View and modify data records in the database
2.JDBC Environment configuration
- JAVA(JDK) install
- Installation of database system ( Such as MySQL,SQL Server Installation )
3. establish JDBC Applications
Build a JDBC Applications , This tutorial uses java Connect SQL Server For example , It is carried out in six steps :
1. Import package
Include in the program what is needed for database programming JDBC class . Most of the time , Use import java.sql.* That's enough
2. register JDBC The driver
Initialize the driver , Open communication with the database .
Class.forName("com.mysql.cj.jdbc.Driver");
3. Open the link
Use DriverManager.getConnection() Method to create a Connection object ,
String user="sa";
String pwd="123456";
System.out.println(" Connect to database ...");
Connection con=DriverManager.getConnection(url,user,pwd);
4. Execute a query
Use one Statement Type or PreparedStatement The object of , And submit a SQL Statement to the database to execute the query :
System.out.println(" establish statement...");
Statement sm=con.createStatement();
String sql="SELECT * FROM Students";
ResultSet rs=sm.executeQuery(sql);
If you want to execute a SQL sentence :UPDATE,INSERT,DELETE sentence , as follows :
sm=con.createStatement();
String sql="DELETE FROM Students";
ResultSet rs=sm.executeUpdate(sql);
5. Extract data from the result set
This step demonstrates how to get the data of query results from the database . You can use the appropriate ResultSet.getXXX() Method to retrieve the data, and the results are as follows :
//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. Clean up environmental resources
In the use of JDBC After interacting with the data in the database , All database resources should be explicitly shut down to reduce the waste of resources , Depend on JVM The garbage collection of is as follows :
//STEP 6: Clean-up environment
rs.close();
stmt.close();
con.close();
Pay attention and praise to support bloggers ~
边栏推荐
- CDN acceleration and cracking anti-theft chain function
- 作者已死?AI正用艺术征服人类
- 顶测分享:想转行,这些问题一定要考虑清楚!
- NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
- RichView TRVStyle 模板样式的设置与使用
- [server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
- leetcode35. 搜索插入位置(简单,找插入位置,不同写法)
- Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
- 网络安全基础介绍
- Misc of BUU (update from time to time)
猜你喜欢

UWA pipeline version 2.2.1 update instructions

Raspberry pie 3B update VIM

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

Lesson 12 study notes 2022.02.11

Short video, more and more boring?

ROS learning_ Basics

You deserve this high-value open-source third-party Netease cloud music player

因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫

《从0到1:CTFer成长之路》书籍配套题目(周更)

idea控制台彩色日志
随机推荐
Applied stochastic process 01: basic concepts of stochastic process
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
Cif10 actual combat (resnet18)
leetcode1020. Number of enclaves (medium)
What is the biggest problem that fresh e-commerce is difficult to do now
1091: two or three things in childhood (multi instance test)
The author is dead? AI is conquering mankind with art
SEO学习的最好方式:搜索引擎
Development of entity developer database application
Raspberry pie serial port login and SSH login methods
Depth residual network
JDBC学习笔记
[advanced software testing step 1] basic knowledge of automated testing
Zhongqing reading news
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
这个高颜值的开源第三方网易云音乐播放器你值得拥有
Lesson 12 study notes 2022.02.11
leetcode6109. 知道秘密的人数(中等,周赛)
[daily question] 729 My schedule I
呆错图床系统源码图片CDN加速与破解防盗链功能