当前位置:网站首页>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
边栏推荐
猜你喜欢

Apache Doris becomes the top project of Apache

1181:整数奇偶排序

Write a simple timeline

Expérience d'optimisation SQL: de 30248 secondes à 0001 secondes

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

Linux下安装redis 、Windows下安装redis(超详细图文教程)

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

Discussion on the improvement and application of the prepayment system in the management of electricity charge and price

Explain observer mode

spark的资源调度和任务调度
随机推荐
Music website design based on harmonyos (portal page)
Two interview demo
Calculation of stock purchase and sale expenses
买卖股票费用计算
Container adapter - stack: stack queue: queue priority_ Queue: priority queue
Implementation of single sign on
Divide and rule classic Hanoi
1. Kimball dimension modeling of data warehouse: what is a fact table?
股票 停牌
为什么SELECT * 会导致查询效率低?
Discussion on safety management of centralized maintenance construction site of substation under the mode of operation and maintenance
Analysis of prepaid power purchase device
DEJA_ Vu3d - 052 of cesium feature set - Simulation of satellite orbit (high altitude) effect
APICloud携手三六零天御,助力企业守好App安全“第一关”
Implement global double finger long press to return to the desktop
Applet: traverse the value of an array in the list, which is equivalent to for= "list" list An item in comment
Postman interface test
redis5.0的槽点迁移,随意玩(单机迁移集群)
PMP needs to master its own learning methods
[share OpenGL tutorial]