当前位置:网站首页>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();
边栏推荐
- 1. Color inversion, logarithmic transformation, gamma transformation source code - miniopencv from zero
- Golang DNS 随便写写
- How to use information mechanism to realize process mutual exclusion, process synchronization and precursor relationship
- Online yaml to CSV tool
- Verrouillage [MySQL]
- PHP - Common magic method (nanny level teaching)
- Introduction to number theory (greatest common divisor, prime sieve, inverse element)
- 灰度升级 TiDB Operator
- Leetcode question brushing (5.31) string
- Leetcode question brushing (5.28) hash table
猜你喜欢

将 NFT 设置为 ENS 个人资料头像的分步指南

根据csv文件某一列字符串中某个数字排序

ESP系列引脚說明圖匯總

2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers

【MySQL】日志

All the ArrayList knowledge you want to know is here

Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund

Beijing invitation media

2022.02.13 - NC001. Reverse linked list
![[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached](/img/35/898a8086bc35462b0fcb9e6b58b86b.jpg)
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
随机推荐
Use br to back up tidb cluster data to S3 compatible storage
Leetcode question brushing record | 203_ Remove linked list elements
Hill sort c language
[cloud native] teach you how to build ferry open source work order system
2022 Inner Mongolia latest water conservancy and hydropower construction safety officer simulation examination questions and answers
JS select all and tab bar switching, simple comments
Leetcode skimming (5.29) hash table
From monomer structure to microservice architecture, introduction to microservices
Upgrade tidb with tiup
Let the bullets fly for a while
Golang force buckle leetcode 1020 Number of enclaves
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
sys.argv
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
Huawei cloud OBS file upload and download tool class
vulnhub hackme: 1
【MySQL】数据库的存储过程与存储函数通关教程(完整版)
Pyqt5 development tips - obtain Manhattan distance between coordinates
[untitled]
使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据