当前位置:网站首页>How to use JDBC to operate database
How to use JDBC to operate database
2022-07-28 03:25:00 【Like the rising sun】
List of articles
With Mysql For example , Let's see Java How to link with the database
1. Import database driver jar package
The first step is to download a mysql Database driver for jar package , similar mysql-connector-java-5.1.20-bin.jar. Create a new one JAVA project , Create a... Under the project lib Folder , Will drive jar File copy to lib In the folder .
Then you need to put this JAR Package added to project's build path variable . Right mouse button -build path->configure build path … Click... In the pop-up interface Add jars Select drive jar Package add it to build path variable .
2. Registration drive
Imported driver , Then you need to register the driver in the program , This means that the program can operate the database corresponding to this driver .
Class.forName(driver);
This sentence will be abnormal , Then the processing method is very simple , Just throw an exception when defining the class , perhaps try/catch Can
Just like this.
public void QueeyAll() throws Exception{
}
3. Get database connection object Connection
The connection object is java Bridge between code and database , Through this connection object ( bridge ) Connect to database .
String driver = "com.mysql.jdbc.Driver";//MySQL Driver name of the database
String url = "jdbc:mysql://localhost:3306/oldsix?useUnicode=true&characterEncoding=utf- 8&useSSL=false";// Database connection string
String user = "root";// user name
String password = "******";// password
Connection conn = DriverManager.getConnection(url,user,password);
The user name and password are the account and password of the database you use
4. Definition SQL sentence
Connected to the database , Is to operate the database . So define SQL sentence , In fact, it's what you want to do with the database , Put it in SQL In the sentence ( An incompletely correct metaphor ,SQL Statements are containers for operations on databases ).
For example, I want to query user All the data in the table
ResultSet result = null;
String sql = "select * from user";
result = stmt.executeQuery(sql);
5. Access to perform SQL Object of statement Statement
Define the operations to be performed on the database , Then who will help me execute ?—— perform SQL Object of statement Statement. So get this object .
Statement stmt = null;
stmt = conn.createStatement();
6. perform SQL, Receive the returned result , Processing return results
With the execution object, you can start to perform operations on the database , And return the operation result . The user processes the returned results
ResultSet result = null;
result = stmt.executeQuery(sql);
7. Release resources
After performing a series of operations , Then release resources . It's equivalent to someone working for you , When you're done , Let others go .
result.close();
stmt.close();
conn.close();
After finishing these tasks, we can operate the database
边栏推荐
- 力扣(LeetCode)208. 实现 Trie (前缀树)(2022.07.27)
- Summary of redis classic interview questions
- Stm32f407 ------- FPU learning
- 在线问题反馈模块实战(十六):实现查详情功能
- Color recognition method and exploration based on MATLAB
- Blue Bridge Cup: the ninth - "lantern controller"
- IO analog serial port of stm32
- IronOCR for .NET 2022.8
- [2022 Niuke Game 2 J question link with arithmetic progress] three part set three part / three part extreme value / linear equation fitting least square method
- Redis持久化机制
猜你喜欢
![[download file] uniapp develops small programs, downloads files and saves them locally](/img/0f/4963178e44f58ad6e3097844bb6ffd.png)
[download file] uniapp develops small programs, downloads files and saves them locally

响应式高端网站模板源码图库素材资源下载平台源码

Softek Barcode Reader 9.1.5

【下载文件】uniapp开发小程序,下载文件并保存到本地

Scheme sharing | experts gather to jointly explore accent AI speech recognition

Redis persistence mechanism

【2022 牛客第二场J题 Link with Arithmetic Progression】三分套三分/三分极值/线性方程拟合最小二乘法

mysql存储过程 使用游标实现两张表数据同步数据

53. Maximum Subarray最大子数组和

STM32 RT-Thread虚拟文件系统挂载操作
随机推荐
Redis实现分布式锁
20条心灵鸡汤唯美句子,句句温情暖心!
【类的本质(Objective-C语言中)】
PCB丝印如何摆?请查收这份手册!
Shell编写规范和变量
IronOCR for .NET 2022.8
redis网络模型解析
【ACwing 1064 小国王】状压dp
xctf攻防世界 Web高手进阶区 unserialize3
数字孪生技术驱动智能工厂减负赋能提升运维效益
MySQL的碎片有哪些
max_ pool2d(): argument ‘input‘ (position 1) must be Tensor, not NoneType
c#——switch case语句
C -- switch case statement
C#设置Textbox控件不可编辑
在线问题反馈模块实战(十六):实现查详情功能
Summary of redis classic interview questions
ssm整合(整合配置)
Uniapp - make phone calls and send text messages
Redis memory recycling