当前位置:网站首页>[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 ~
边栏推荐
- 接口自动化测试框架:Pytest+Allure+Excel
- Three methods of adding color to latex text
- The difference between get and post request types
- Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
- Internal and external troubles of "boring ape" bayc
- Top test sharing: if you want to change careers, you must consider these issues clearly!
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- leetcode704. 二分查找(查找某个元素,简单,不同写法)
- The psychological process from autojs to ice fox intelligent assistance
- First knowledge of OpenGL es learning (1)
猜你喜欢
树莓派串口登录与SSH登录方法
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
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
leetcode704. Binary search (find an element, simple, different writing)
Introduction to ros2 installation and basic knowledge
idea控制台彩色日志
OpenGL ES 学习初识(1)
这个高颜值的开源第三方网易云音乐播放器你值得拥有
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
ROS learning_ Basics
随机推荐
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
CDN acceleration and cracking anti-theft chain function
The first Baidu push plug-in of dream weaving fully automatic collection Optimization SEO collection module
因高额网络费用,Arbitrum 奥德赛活动暂停,Nitro 发行迫在眉睫
PCL realizes frame selection and clipping point cloud
19. Actual memory management of segment page combination
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
Cookie Technology & session Technology & ServletContext object
【Hot100】739. 每日溫度
Interface automation test framework: pytest+allure+excel
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
How to configure GUI guide development environment
作者已死?AI正用艺术征服人类
巴比特 | 元宇宙每日必读:中国互联网企业涌入元宇宙的群像:“只有各种求生欲,没有前瞻创新的雄心”...
Depth residual network
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
Compile, connect -- notes-2
前缀和数组系列
接口自动化测试框架:Pytest+Allure+Excel
LeetCode Algorithm 2181. 合并零之间的节点