当前位置:网站首页>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
边栏推荐
- 魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
- 每日一题冲刺大厂第十六天 NOIP普及组 三国游戏
- Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
- Usage of vector common interface
- Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
- How big the program development of single chip microcomputer project can be, it represents your level of knocking code
- Rhcsa day 3 operation
- Applet canvas canvas half ring
- 1215_ Hello world used by scons
- Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
猜你喜欢

基于Torchserve部署SBERT模型<语义相似度任务>

Fashion MNIST 数据集分类训练

Talk about biology live broadcast: Dr. Wang Ziyuan, a lake view biology, exploring hepatitis B with gene therapy

How to solve the complexity of project document management?

011_ Redistemplate operation hash
![[chrome extender] content_ Cross domain problem of script](/img/be/7b73adc199b9c863bb5cf5a88df324.png)
[chrome extender] content_ Cross domain problem of script

定了,今日起,本号粉丝可免费参与网易数据分析培训营!

手写内存池以及原理代码分析【C语言】

Jupyter for local and remote access to ECS

This is how the power circuit is designed
随机推荐
从0开始开发一个chrome插件(2)
How to prevent leakage of operation and maintenance data
How to recognize products from the perspective of Dialectics
JSP page nesting
Day 3 of learning rhcsa
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
Talk about current limiting
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
Talk about biology live broadcast: Dr. Wang Ziyuan, a lake view biology, exploring hepatitis B with gene therapy
JVM运行时数据区(一)
IAR嵌入式開發STM32f103c8t6之點亮LED燈
1215_ Hello world used by scons
[nk]牛客月赛48 D
Web learning record (medium)
Understanding process (multithreading primary)
Program design: l1-49 ladder race, allocation of seats (simulation), Buxiang pill hot
WordPress calls the specified page content. 2 get_ children()
The importance of ERP integration to the improvement of the company's system
Applet reads files
Day 4 of learning rhcsa