当前位置:网站首页>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/Jhttps://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
边栏推荐
- P2394 yyy loves Chemistry I
- Virtual machine 14 installing win7 (Figure tutorial)
- Batch modify tables and sorting rules for fields in tables
- Common test method used by testers --- orthogonal method
- Implementation of single sign on
- Protection range and optimization of motor protector for hoist equipment
- Music website design based on harmonyos (portal page)
- Learn how Alibaba manages the data indicator system
- 1. Kimball dimension modeling of data warehouse: what is a fact table?
- [big case] Xuecheng online website
猜你喜欢
This article explains in detail the difficult problems and solutions faced by 3D cameras
P2394 yyy loves Chemistry I
JMeter -- interface test 1
Chrome devtools
股票 停牌
Apache Doris becomes the top project of Apache
Fire fighting work and measures in Higher Vocational Colleges
SQL 優化經曆:從 30248秒到 0.001秒的經曆
Full link service tracking implementation scheme
P2394 yyy loves Chemistry I
随机推荐
Discussion on the improvement and application of the prepayment system in the management of electricity charge and price
DEJA_ Vu3d - 051 of cesium function set - perfect realization of terrain excavation
股票 停牌
手机买同业存单基金开户选哪家证券公司比较好,比较安全呢
理解IO模型
Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance
1181: integer parity sort
Application of current limiting protector in preventing electrical fire in shopping malls
Installation of containerd1.5.5
[share OpenGL tutorial]
自动转换之-面试题
A strange execution plan. One table in the association query is not associated with any other tables
Two interview demo
Redis5.0 slot migration, free play (single machine migration cluster)
Summary of PMP learning experience
Basic content learning of software testing (I)
Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
The Cassandra cluster reinstalls and starts from the node. An error is reported. There is an existing solution
JMeter -- interface test 1
STL - inverter