当前位置:网站首页>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
边栏推荐
- 012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
- RT_Thread自问自答
- SO库依赖问题
- 期中汇总(概论+应用层+运输层)
- Execute external SQL script in MySQL workbench and report error
- Jupyter for local and remote access to ECS
- Looking for a job, writing a resume to an interview, this set of information is enough!
- Renesas electronic MCU RT thread development and Design Competition
- Kubernetes cluster installation based on Kirin SP10 server version
- Halo open source project learning (I): project launch
猜你喜欢
随机推荐
php+mysql对下拉框搜索的内容修改
C语言 171. 最近回文数
TypeScript(1)
tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
IAR embedded development stm32f103c8t6 Lighting LED
[untitled]
能做多大的单片机项目程序开发,就代表了你的敲代码的水平
How to recognize products from the perspective of Dialectics
Synchronized锁及其膨胀
This is how the power circuit is designed
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
Usage of vector common interface
PHP sorting of interview questions on April 20, 2022
Rhcsa second day operation
[suggestion collection] hematemesis sorting out golang interview dry goods 21 questions - hanging interviewer-1
[XJTU計算機網絡安全與管理]第二講 密碼技術
Target narak
智能辅助功能丰富,思皓X6安全配置曝光:将于4月23日预售
day18--栈队列
Efficient music format conversion tool Music Converter Pro




![Handwritten memory pool and principle code analysis [C language]](/img/9e/fdddaa628347355b9bcf9780779fa4.png)



