当前位置:网站首页>Explanation of JDBC classes
Explanation of JDBC classes
2022-07-28 10:59:00 【Camellia——】
JDBC Operation steps of :
1) Registration drive
java.sql.DriverManager: Driver management ( Manage a group of JDBC Service )
Registration driven method :
public static void registerDriver(Driver driver):
Formal parameter passing It's the interface , You need to change the sub implementation class object of the interface
Drive provided Jar package :com.mysql.jdbc.Driver implements java.sql.Driver Interface
Normal format : ( disadvantages : It's equivalent to registering twice )
Driver driver = new com.mysql.jdbc.Driver() ;
// DriverManager.registerDriver(driver);
Bottom source :
Package name :com.mysql.jdbc
public class Driver implements java.sql.Driver {
Static code block : Load the Driver Class , Statically related code is executed first !
static {
try {
// Use the driver management to register the driver
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
}
After the optimization : Using the idea of reflection : Just load com.mysql.jdbc.Driver This class is ok !
Class.forName("com.mysql.jdbc.Driver") ;// Ensure the downward compatibility of versions , It is suggested to always give this sentence !
2) Drive management DriverManager Get the connection object of the database :Connection(java.sql)
Drive management DriverManger : Manage a group of JDBC Service
public static Connection getConnection(
String url,String user,String password): Get the connection
url: Uniform resource locator : Namely uri A subset of
Use url:
jdbc:mysql:// domain name : Port number / Database name ( The library must exist !)
There are a lot of agreements :
ftp://
http:// domain name : Port number / Specific request address for access
mail: Email protocol
thunder:// Thunder Association ...
file:// Local file protocol
...
uri: Network resources are a general term !
http://localhost:8080/login :URL
/login: uri
user: user name : The name of the user connecting to the database :root
password: The password used to connect to the database
3)Connection: Connection object java.sql.Connection
Represents a session with a database ( Connect ), You can get the metadata information of the current linked database ( The structure of the table / The field type of the table ..)
Member method :
Statement createStatement(): establish Statement Perform object
And will specify sql Send to the specified database
PreparedStatement prepareStatement(String sql): The parameter sql Statement to execute
Send to database , And get the precompiled object
DatabaseMetaData getMetaData(): Get metadata information related to the database
It includes the structure of some databases and tables ....
4)Statement:java.sql.Statement
Perform object
Execute static sql sentence
give an example :
"update account set balance = balance + 500 where id = 1;
id Value , to balance+500 Value ,( Hard encoding )
Member method :
int executeUpdate(String sql): Common methods
in the light of DML sentence :insert,update,delete, With returned results
Can perform the most basic DDL sentence , There is no content returned !
ResultSet executeQuery(String sql): Special execution DQL sentence :select..
The result returned : Result set object ( Data table of database result set )
5)PreparedStatement Perform object ( Precompiled objects ) extends Statement
precompiled SQL Object of statement .
Give parameterized sql Assign a value
sql:
insert into Table name (name,age,gender) values(?,?,?) ; Placeholder
publi void setXXX( The first parameter , actual value ) ;
ResultSet executeQuery(): perform DQL sentence
int executeUpdate(): perform DML Statement will return the result : Affected the number of rows
Appoint DDL sentence , The return is 0
6)ResultSet: The data table of the result set generated after the query (java.sql.xx)
边栏推荐
猜你喜欢

ICML 2022 | graph represents the structure aware transformer model of learning

6. MapReduce custom partition implementation

剑指 Offer 06. 从尾到头打印链表

11_ UE4 advanced_ Change male characters to female characters and modify the animation

Table data processing software, what else besides excel?

PyQt5快速开发与实战 4.12 日历与时间

哈希表的相关知识点

学会这些分析方法及模型,遇到问题不再没思路

Ten questions about low code: tell everything about low code!

适合中小企业的进销存软件,搞定5大难题
随机推荐
Redis-day01-常识补充及redis介绍
剑指 Offer 30. 包含min函数的栈
蓝桥杯嵌入式-HAL库-ADC
PyQt5快速开发与实战 4.13 菜单栏、工具栏与状态栏 and 4.14 QPrinter
Apb2 and apb1 in stm32
samba学习
21. Merge two ordered linked lists
MySQL Architecture Principle
Pyqt5 rapid development and practice 4.12 calendar and time
Tree shaking and DCE
GKBillowNoiseSource
02.1.2. logic type bool
Status Notice ¶
吊打面试官的问题
剑指 Offer 09. 用两个栈实现队列
Invalid ROM Table原因及解决办法
Inventory: exciting data visualization chart
PyQt5快速开发与实战 4.12 日历与时间
GKCheckerboardNoiseSource
Blue Bridge Cup embedded Hal library USART_ TX