当前位置:网站首页>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
边栏推荐
猜你喜欢

53. Maximum Subarray最大子数组和

RBD块存储设备的扩容以及缩容操作(六)

MySQL的碎片有哪些

Redis memory recycling

数据湖(十七):Flink与Iceberg整合DataStream API操作

Thread Foundation

Raspberry pie development relay control lamp

Comprehensive comparative study of image denoising

Engineering Geology Practice - engineering geology problem set

如何一键进行重装Win11系统
随机推荐
ThreadLocal usage scenario
C -- switch case statement
20 soul chicken soup beautiful sentences, sentence by sentence warm heart!
Stm32f407 ------- FPU learning
Blue Bridge Cup: the ninth - "lantern controller"
如何卸载干净zabbix服务?(超详细)
图文并茂:JVM 内存布局详解
Engineering Geology Practice - engineering geology problem set
ELS timer
LeetCode 第二十九天
机器人开发--丝杠与导轨
[SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
4天Excel实战训练营,0.01元特惠仅三天,赠200套学习资料包
[nature of class (in Objective-C language)]
4、 Analysis of solid state disk storage technology (paper)
Thread Foundation
将Helm编写的Chart推送到Harbor仓库
C WinForm development: how to add pictures to project resources
Summary of concurrent programming interview questions
Scheme sharing | experts gather to jointly explore accent AI speech recognition