当前位置:网站首页>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();
边栏推荐
- 2. File operation - write
- The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
- 使用 TiDB Lightning 恢复 S3 兼容存储上的备份数据
- 1204 character deletion operation (2)
- Image fusion -- challenges, opportunities and Countermeasures
- Analysis of pointer and array written test questions
- IOT -- interpreting the four tier architecture of the Internet of things
- 图像融合--挑战、机遇与对策
- leetcode刷题 (5.28) 哈希表
- The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
猜你喜欢
Online yaml to CSV tool
hcip--mpls
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
Résumé des diagrammes de description des broches de la série ESP
Learn Arduino with examples
Make learning pointer easier (3)
2022 Inner Mongolia latest construction tower crane (construction special operation) simulation examination question bank and answers
[research materials] 2021 China online high growth white paper - Download attached
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
随机推荐
Résumé des diagrammes de description des broches de la série ESP
[research materials] 2022 China yuancosmos white paper - Download attached
Hill sort c language
NFT smart contract release, blind box, public offering technology practice -- contract
Leetcode question brushing record | 203_ Remove linked list elements
LDAP application (4) Jenkins access
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
IOT -- interpreting the four tier architecture of the Internet of things
C language custom type: struct
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
Deep learning: derivation of shallow neural networks and deep neural networks
Ruffian Heng embedded bimonthly, issue 49
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
1202 character lookup
[untitled]
Use Alibaba icon in uniapp
IP lab, the first weekly recheck
你想知道的ArrayList知识都在这
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
LDAP應用篇(4)Jenkins接入