当前位置:网站首页>[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 ~
边栏推荐
- 微信脑力比拼答题小程序_支持流量主带最新题库文件
- 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
- 作者已死?AI正用艺术征服人类
- GET 和 POST 请求类型的区别
- How to find a medical software testing institution? First flight software evaluation is an expert
- Wechat official account infinite callback authorization system source code, launched in the whole network
- leetcode841. 钥匙和房间(中等)
- The best way to learn SEO: search engine
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- 19.段页结合的实际内存管理
猜你喜欢
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
Hydra common commands
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
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
Fast target recognition based on pytorch and fast RCNN
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models. common‘ from ‘/home/yolov5/models/comm
顶测分享:想转行,这些问题一定要考虑清楚!
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
变量的命名规则十二条
随机推荐
The differences and advantages and disadvantages between cookies, seeion and token
微信脑力比拼答题小程序_支持流量主带最新题库文件
Cif10 actual combat (resnet18)
【JDBC】快速入门教程
The author is dead? AI is conquering mankind with art
BIO模型实现多人聊天
《从0到1:CTFer成长之路》书籍配套题目(周更)
Visitor tweets about how you can layout the metauniverse
呆错图床系统源码图片CDN加速与破解防盗链功能
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
【Hot100】739. Daily temperature
What does UDP attack mean? UDP attack prevention measures
GET 和 POST 请求类型的区别
What is the biggest problem that fresh e-commerce is difficult to do now
leetcode1020. Number of enclaves (medium)
多线程和并发编程(二)
Setting and using richview trvstyle template style
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
攻防世界 MISC中reverseMe简述