当前位置:网站首页>JDBC JDBC
JDBC JDBC
2022-04-23 02:38:00 【Statichit static smash】
DriverManager: Used to manage JDBC Driven service class . The main function of using this class in the program is obtain Connection object , This class contains the following methods :
public static synchronized Connection getConnection(String url,String user,String pass)throws SQLException
// This method obtains url The connection to the database
Connection: representative Database connection object , Every Connection Represents a physical connection session . Want to access the database , You have to get a database connection first . The common methods of this interface are as follows .
Statement creatStatement()throws SQLExcetpion
// This method returns a Statement object
JDBC Programming steps ( Six steps )
1. The load driver
2. Get the connection
3. Get database operation object
4. perform sql
5. Process query result set
6. Release resources
1. Load database driver
Usually use Class Class forName() Static method to load the driver . for example
Class.forName(driverClass)
In code driverClass Is the string corresponding to the database driver class .
load MYSQL The driver :
Class.forName("com.mysql.cj.jdbc.Driver");
2. Get the connection
adopt DriverManager Get database connection .
// Get database connection
DriverManager.getConnection(String url,String user,String pass);
URL: database
database URL Usually follow the following wording
jdbc:subprotocol:other stuff
above URL In writing jdbc Is constant , and subprotocol Specifies the driver that connects to a specific database , And then there's other stuff It's not fixed either . for example MYSQL Database URL It is written as follows :
jdbc:mysql://hostname:port/databasename
url What are the components of :
agreement ,IP, Port number , Resource namefor example : jdbc:mysql://localhost:3366/bjpowernode
jdbc:mysql:// agreement
localhost IP Address
3366 Port number
chatroom Database instance name
3. Get database operation object
adopt Connection objects creating Statement object .
Connection establish Statement There are three ways
creatStatement(): Create basic Statement object
prepareStatement(String sql): Based on the incoming SQL Statement to create precompiled Statement object
prepareCall(String sql): Based on the incoming SQL Sentence creation CallableStatement object
take 3.4. Operation merge :
eg:
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/xxx", "root", "123456"); // What you enter here is the database name xxx Statement state=con.createStatement();
4. perform sql
Use Statement perform SQL sentence . be-all Statement There are three ways to execute SQL sentence .
execute(): You can do anything SQL sentence . But it's troublesome .
executeUpdata(): Mainly used for execution DML and DDL sentence . perform DML Statement to return by SQL Number of rows affected , perform DDL Statement returns 0.
executeQuery(): Only query statements can be executed , After execution, it returns the query result ResultSet object .
5. Process query result set
If executed SQL Statement is a query statement , The execution structure will return a ResultSet object . The object contains SQL Structure of statement query , The program can operate the REsultSet Object to retrieve the query results .( Get data as needed according to the situation )
6. Release resources
Reclaim database resources , Including closing ResultSet,Statement and Connection And so on .
版权声明
本文为[Statichit static smash]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230231236411.html
边栏推荐
- Rhcsa day 4 operation
- Flink stream processing engine system learning (II)
- 1215_ Hello world used by scons
- How to recognize products from the perspective of Dialectics
- 007_ Redis_ Jedis connection pool
- [nk]牛客月赛48 D
- Day 4 of learning rhcsa
- The importance of ERP integration to the improvement of the company's system
- 每日一题(2022-04-22)——旋转函数
- Applet reads files
猜你喜欢
Rhcsa day 1 operation
After idea is successfully connected to H2 database, there are no sub files
想体验HomeKit智能家居?不如来看看这款智能生态
How many steps are there from open source enthusiasts to Apache directors?
Flink stream processing engine system learning (I)
16、 Anomaly detection
011_ Redistemplate operation hash
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
打靶narak
VMware virtual machine installation openwrt as side route single arm route img image to vmdk
随机推荐
The usage and difference of * and & in C language and the meaning of keywords static and volatile
Latin goat (20204-2022) - daily question 1
[untitled]
Kubernetes cluster installation based on Kirin SP10 server version
So library dependency
IAR embedded development stm32f103c8t6 Lighting LED
打靶narak
Rhcsa second day operation
解决 注册谷歌邮箱 gmail 手机号无法用于验证
高效音乐格式转换工具Music Converter Pro
011_ Redistemplate operation hash
数仓建表111111
Renesas electronic MCU RT thread development and Design Competition
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
Consider defining a bean of type 'com netflix. discovery. AbstractDiscoveryClientOptionalArgs‘
006_ redis_ Jedis quick start
arduino esp8266 网络升级 OTA
下载正版Origin Pro 2022 教程 及 如何 激 活
[xjtu Computer Network Security and Management] session 2 Cryptographic Technology
1、 Sequence model