当前位置:网站首页>[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 ~
边栏推荐
猜你喜欢

C language_ Double create, pre insert, post insert, traverse, delete

攻防世界 MISC中reverseMe简述

leetcode704. Binary search (find an element, simple, different writing)

Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet

ROS学习_基础

MPLS experiment

变量的命名规则十二条

Fast target recognition based on pytorch and fast RCNN

Cookie Technology & session Technology & ServletContext object

开源的网易云音乐API项目都是怎么实现的?
随机推荐
Kubernetes cluster builds ZABBIX monitoring platform
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
UWA pipeline version 2.2.1 update instructions
首发织梦百度推送插件全自动收录优化seo收录模块
“无聊猿” BAYC 的内忧与外患
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
Embed UE4 program into QT interface display
Cif10 actual combat (resnet18)
Internal and external troubles of "boring ape" bayc
3. Business and load balancing of high architecture
《从0到1:CTFer成长之路》书籍配套题目(周更)
(4) Web security | penetration testing | network security web site source code and related analysis
微信脑力比拼答题小程序_支持流量主带最新题库文件
Depth residual network
supervisor 使用文档
The best way to learn SEO: search engine
【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
Entity Developer数据库应用程序的开发
Raspberry pie serial port login and SSH login methods
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)