当前位置:网站首页>JDBC connection database (MySQL) steps
JDBC connection database (MySQL) steps
2022-06-28 09:21:00 【get-yuan】
First, the first step is to mysql Built in database a surface
CREATE TABLE a(
`id` INT PRIMARY KEY,
`NAME` VARCHAR(40),
`PASSWORD` VARCHAR(40),
`email` VARCHAR(60),
`birthday` DATE
); The second step is idea Introduction in mysql-connector-java.jar package , Download address of the package :MySQL :: Download Connector/J
https://dev.mysql.com/downloads/connector/j/?os=26 The way to import packages is : First, unzip the downloaded package , find mysql-connector-java.jar package , And copy the package to the project
lib Under the directory of ,lib The directory needs to be created by yourself


Then right-click lib Catalog , Click on Add as Library The import is really successful

Then start connecting to the database in the project
public class JdbcFirstDemo {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1、 The load driver ( Fixed writing )
Class.forName("com.mysql.jdbc.Driver");
//2、 User information 、URL( Fixed writing )
String url="jdbc:mysql://localhost:3306/a?useUnicode=true&useCharacter=utf8&useSSL=true";
String user="root";// user name
String pwd = "123456";/ User password
//3、 Successful connection , Database objects ( Fixed writing )
Connection connection = DriverManager.getConnection(url, user, pwd);
//4、 perform sql The object of ( Fixed writing )
Statement statement = connection.createStatement();
//5、 perform sql sentence , Return result set
String sql="select * from a";//sql Query statement
ResultSet resultSet = statement.executeQuery(sql);
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("birth="+resultSet.getObject("birthday"));
}// Output results
// Release the connection ( Fixed writing )
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
边栏推荐
- How to solve the problem of port number occupation
- APICloud携手三六零天御,助力企业守好App安全“第一关”
- Chrome devtools
- 怎样在手机上开户?现在网上开户安全么?
- Linux下安装redis 、Windows下安装redis(超详细图文教程)
- 手机买同业存单基金开户选哪家证券公司比较好,比较安全呢
- Is it safe for CICC fortune to open an account? How much do you charge?
- Data modeling based on wide table
- Calculation of stock purchase and sale expenses
- Key points of building fire protection design
猜你喜欢

Boundary value analysis method for learning basic content of software testing (2)

STL -- binder

Prototype chain JS

Using transform:scale causes the page mouse hover event to disappear

1182: effets de la photo de groupe

虛擬機14安裝win7(圖教程)

学习阿里如何进行数据指标体系的治理

SQL注入之文件读写

Test cases for learning the basic content of software testing (II)

全链路业务追踪落地实践方案
随机推荐
"Jianzhi offer" -- Interview Question 4: finding two-dimensional arrays
Calcul des frais d'achat et de vente d'actions
Copy & Deepcopy
Valentine's Day - VBS learning (sentences, love words)
From knowledge to wisdom: how far will the knowledge map go?
Campus honey decoration of APP course design (e-commerce platform)
Implementation of code scanning login
I want to register my stock account online. How do I do it? Is online account opening safe?
Machine virtuelle 14 installer win7 (tutoriel)
Rman Backup Report Ora - 19809 Ora - 19804
Stock suspension
Two interview demo
Automatic conversion - interview questions
买卖股票费用计算
小程序 :遍历list里面的某个数组的值,等同于 for=“list” list.comment里面的某一项
Discussion on the improvement and application of the prepayment system in the management of electricity charge and price
股票 停牌
Scenario method and error recommendation method for learning basic content of software testing (2)
构造方法绝不是在new()之后就立马执行!!!!!
Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance