当前位置:网站首页>MySQL learning record 10getting started with JDBC
MySQL learning record 10getting started with JDBC
2022-07-06 08:26:00 【Jatine】
List of articles
MySQL Learning record 10JDBC introduction
10.1 Database driven
The program transfers data through database driver and database 
And suppose we have MySQL、Oracle、Sql Server Etc , Are we going to install multiple drivers ? That's when JDBC.
10.2 What is? JDBC
JDBC(Java DataBase Connectivity) Namely Java Database connection , To put it bluntly, it is to use Java Language to operate the database . It turns out that we operate the database in the console SQL Statement to operate on the database ,JDBC Yes, it is Java Language sends to the database SQL sentence .

10.3 first JDBC Program
1、 Create a project , Create test database
CREATE DATABASE `jdbcStudy` CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `jdbcStudy`;
CREATE TABLE `users`(
`id` INT PRIMARY KEY,
`NAME` VARCHAR(40),
`PASSWORD` VARCHAR(40),
`email` VARCHAR(60),
birthday DATE
);
INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)
VALUES(1,'zhangsan','123456','[email protected]','1980-12-04'),
(2,'lisi','123456','[email protected]','1981-12-04'),
(3,'wangwu','123456','[email protected]','1979-12-04')
2、 Import database driver 
3、 Write test code
import java.sql.*;
// first jdbc Program
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1. The load driver
Class.forName("com.mysql.jdbc.Driver"); // Fixed writing , The load driver
//2. User information and url
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false";
String username = "root";
String password = "123456";
//3. Successful connection Connection On behalf of the database
Connection connection = DriverManager.getConnection(url, username, password);
//4. perform sql The object of
Statement statement = connection.createStatement();
//5. perform sql
String sql = "SELECT * FROM users";
ResultSet resultSet = statement.executeQuery(sql);// Return result set The result set encapsulates all the results of our query
while (resultSet.next()) {
System.out.println("id=" + resultSet.getObject("id"));
System.out.println("name=" + resultSet.getObject("NAME"));
System.out.println("password=" + resultSet.getObject("PASSWORD"));
System.out.println("email=" + resultSet.getObject("email"));
System.out.println("birthday=" + resultSet.getObject("birthday"));
}
//6. Release the connection
resultSet.close();
statement.close();
connection.close();
}
}

Step summary :
1、 The load driver
2、 Connect to database DriverManager
3、 Get executed sql The object of Statement
4、 Get the returned result set
5、 Release the connection
DriverManager
Class.forName(“com.mysql.jdbc.Driver”); // Fixed writing , The load driver
Connection connection = DriverManager.getConnection(url, username, password);connection On behalf of the database
Database settings auto commit
URL
String url = “jdbc:mysql:// The host address : Port number / Database name ? Parameters 1& Parameters 2&…”;
mysql The default port number is 3306
Statement
statement.executeQuery();// Query operation return Resultset
statement.execute();// Carry out any sql
statement.executeUpdate();// to update 、 Insert 、 Delete All use this Returns the number of rows affected
ResultSet: Query result set , Encapsulates all query results
Gets the specified data type
resultSet.getObject();// If you don't know the type of column, use this
resultSet.getString();// Know the type of column and use the corresponding
resultSet.getInt();
resultSet.getFloat();
resultSet.getDate();
...
Release resources
resultSet.close();
statement.close();
connection.close();
边栏推荐
- Online yaml to CSV tool
- 【MySQL】数据库的存储过程与存储函数通关教程(完整版)
- 2022.02.13 - NC001. Reverse linked list
- Ruffian Heng embedded bimonthly, issue 49
- IOT -- interpreting the four tier architecture of the Internet of things
- Nft智能合约发行,盲盒,公开发售技术实战--合约篇
- Beijing invitation media
- 【MySQL】鎖
- MFC 给列表控件发送左键单击、双击、以及右键单击消息
- From monomer structure to microservice architecture, introduction to microservices
猜你喜欢
![[cloud native] teach you how to build ferry open source work order system](/img/fb/507f763791235bd00bc8201e5d7741.png)
[cloud native] teach you how to build ferry open source work order system

C language - bit segment

Hungry for 4 years + Ali for 2 years: some conclusions and Thoughts on the road of research and development

leetcode刷题 (5.28) 哈希表
![[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached](/img/35/898a8086bc35462b0fcb9e6b58b86b.jpg)
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
![[MySQL] database stored procedure and storage function clearance tutorial (full version)](/img/27/e775e03b77c7195216bc50c5cbefb4.png)
[MySQL] database stored procedure and storage function clearance tutorial (full version)
![[untitled]](/img/38/bc025310b9742b5bf0bd28c586ec0d.jpg)
[untitled]

NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle

2022.02.13 - NC004. Print number of loops

你想知道的ArrayList知识都在这
随机推荐
Learn Arduino with examples
Pyqt5 development tips - obtain Manhattan distance between coordinates
LDAP应用篇(4)Jenkins接入
灰度升级 TiDB Operator
Upgrade tidb operator
从 SQL 文件迁移数据到 TiDB
[MySQL] lock
Wireshark grabs packets to understand its word TCP segment
Migrate data from SQL files to tidb
ESP series pin description diagram summary
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
你想知道的ArrayList知识都在这
Easy to use tcp-udp_ Debug tool download and use
[cloud native topic -45]:kubesphere cloud Governance - Introduction and overall architecture of enterprise container platform based on kubernetes
Nacos Development Manual
NFT smart contract release, blind box, public offering technology practice -- contract
[brush questions] top101 must be brushed in the interview of niuke.com
LDAP應用篇(4)Jenkins接入
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Migrate data from CSV files to tidb