当前位置:网站首页>JDBC connection database (idea version)
JDBC connection database (idea version)
2022-07-26 01:00:00 【Programming Xiaobai who loves Java】
1. Introduce dependencies :
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>2. Code writing :
public static void main(String[] args) throws SQLException {
Connection concon = null;
PreparedStatement pstm = null;
ResultSet resultSet = null;
String url = "jdbc:mysql://localhost:3306/db06";
String user = "root";
String password = "root";
try {
//1. Registration drive .
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//2. Get the connection .
concon = DriverManager.getConnection(url, user, password);
if (!concon.isClosed()) {
System.out.println(" Database connection successful !");
}
//3. Get the preprocessing objects that operate the database
pstm = concon.prepareStatement("select * from t_product");
//4. perform sql, Get the result set
resultSet = pstm.executeQuery();
//5. Traversal result set .
int i = 0;
while (resultSet.next()) {
int id = resultSet.getInt("id");
String productCode = resultSet.getString("product_c1ode");
System.out.println("id" + id);
System.out.println("product_code" + productCode);
}
} catch (SQLException throwables) {
System.out.println(" Database field error !");
} finally {
//6. Release resources
if (resultSet != null) {
resultSet.close();
}
if (pstm != null) {
pstm.close();
}
if (concon != null) {
concon.close();
}
}
}Click on the run , Be accomplished !
边栏推荐
- 【RTOS训练营】任务调度(续)、任务礼让、调度总结、队列和晚课提问
- [RTOS training camp] program framework, preview, after-school homework and evening class questions
- Implementation process of adding loading effect to easycvr page
- MMOCR使用指南
- 【RTOS训练营】关于上课和答疑
- 【RTOS训练营】程序框架、预习、课后作业和晚课提问
- matlab 按位 与 或 非
- Curd used by hyperf
- pip install --upgrade can‘t find Rust compiler
- 数据写入excel并填充颜色
猜你喜欢

Travel + strategy accelerated landing, jietu new product matrix exposure

Nanjie's embarrassment
![[RTOS training camp] problems of evening students](/img/4a/9d781a28751c15e9e42cd5743e97db.jpg)
[RTOS training camp] problems of evening students
![[laser principle and application -3]: foreign brands of lasers](/img/8a/620544bb52c6b8c4db83f9a14783bb.png)
[laser principle and application -3]: foreign brands of lasers

Spine_ Adnexal skin

Upload local file trial version using SAP ui5 fileuploader control
![[RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions](/img/59/0b671129e8cfaed28e2fe484ae0467.jpg)
[RTOS training camp] operation explanation, queue and ring buffer, queue - transmission data, queue - synchronization tasks and evening class questions

开放下载!《阿里巴巴 DevOps 实践手册》

SQL statement exercise

什么是信息化?什么是数字化?这两者有什么联系和区别?
随机推荐
【RTOS训练营】站在更高的角度学习C语言
Save unhappiness! My robot psychologist; KPI tracking of machine learning; Pytorch implementation of Yolo V7; Li Hang's new book "machine learning methods" open trial | showmeai Information Daily
爬虫小操作
Shardingsphere data slicing
Azure synapse analytics Performance Optimization Guide (1) -- optimize performance using ordered aggregate column storage indexes
Processes and threads
聊聊研发团队中的“人”
Leetcode notes 350. Intersection of two arrays II
El table scroll bar settings
开放下载!《阿里巴巴 DevOps 实践手册》
The bumpy road of referencing jar package json-path.jar in jmeter/idea
[RTOS training camp] problems of evening students
南姐的糗事
【RTOS训练营】环形缓冲区、AT指令、预习安排和晚课提问
【秒杀概念】大小端
[oops framework] network module websocket
Detailed explanation of at and crontab commands of RHCE and deployment of Chrony
Test the concept of left shift and right shift
[RTOS training camp] learn C language from a higher perspective
如何mysql只要重复数据?