当前位置:网站首页>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
边栏推荐
- So library dependency
- PTA: praise the crazy devil
- 机器学习(周志华) 第十四章概率图模型
- Renesas electronic MCU RT thread development and Design Competition
- Rich intelligent auxiliary functions and exposure of Sihao X6 security configuration: it will be pre sold on April 23
- Suggestion: block reference sorting is in the order of keywords
- PIP install shutil reports an error
- 006_ redis_ Jedis quick start
- 007_ Redis_ Jedis connection pool
- Leetcode cooking
猜你喜欢

Flink stream processing engine system learning (I)

Real math problems in 1958 college entrance examination
![[XJTU computer network security and management] Lecture 2 password technology](/img/b0/263e8dcbfeb2ce9f504a9c8eb76b07.png)
[XJTU computer network security and management] Lecture 2 password technology

使用Go语言构建Web服务器

Arduino esp8266 network upgrade OTA

Download the genuine origin Pro 2022 tutorial and how to activate it

The usage and difference of * and & in C language and the meaning of keywords static and volatile
![Parental delegation model [understanding]](/img/ba/07e280a2634018e0d6b56c05dd3bc0.png)
Parental delegation model [understanding]

全局、独享、局部路由守卫

双亲委派模型【理解】
随机推荐
一、序列模型-sequence model
数仓建表111111
Daily question (April 22, 2022) - rotation function
The importance of ERP integration to the improvement of the company's system
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
006_ redis_ Jedis quick start
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
Develop a chrome plug-in from 0 (2)
基于Torchserve部署SBERT模型<语义相似度任务>
New book recommendation - IPv6 technology and application (Ruijie version)
JVM class loader
Applet canvas canvas half ring
Rhcsa day 1 operation
Class initialization and instance initialization interview questions
How to solve the complexity of project document management?
arduino esp8266 网络升级 OTA
全局、獨享、局部路由守衛
定了,今日起,本号粉丝可免费参与网易数据分析培训营!
How many steps are there from open source enthusiasts to Apache directors?
Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘