当前位置:网站首页>IDEA connects to MySQL database and uses data
IDEA connects to MySQL database and uses data
2022-07-31 13:48:00 【Is the distance】
一、IDEA连接MySQL数据库
1、First create a new normalJava项目

2、连接数据库
点击右侧DataBase
点击加号,找到MySQL,添加数据库
输入用户名和密码,点击Test Connection
显示连接成功,点击Apply,OK

No database found,点击1 of 7,选择All schemas
All databases are displayed
二、Use database data
1、新建Java类 Test

2、下载MySQL驱动Jar包
方法一:官网下载
官网地址
选择 Plantfrom Independent 版本,下载第二个.
Click Don't log in,只下载
方法二:百度盘
链接:https://pan.baidu.com/s/1yTS6XeTWJ7hm2Z7zPeV_Iw?pwd=qw44
提取码:qw44
3、返回IDEA,新建文件夹lib

解压缩下载的zip文件,把下载好的Jar包放入lib文件夹

4、非常重要(添加到库)
Right-click addedJar包,选择Add as Library(添加到库)
5、编写Test类
import java.sql.*;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
// Using my own database test
String url = "jdbc:mysql://localhost:3306/test";
// 数据库的用户名和密码
String username = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, username, password);
Statement statement = connection.createStatement();
// SQL语句,Use my owntest数据库下的 boss 表
String sql = "SELECT * FROM boss";
ResultSet resultSet = statement.executeQuery(sql);
// It needs to correspond to the table structure in your own database
while (resultSet.next()){
System.out.println("id = " + resultSet.getObject("id"));
System.out.println("name = " + resultSet.getObject("name"));
System.out.println("age = " + resultSet.getObject("age"));
}
resultSet.close();
statement.close();
connection.close();
}
}
boss表结构如下(You need to modify the code for your own table structure)
6、运行主函数,得到数据

三、For more fun and learning content, please pay attention to the public account

边栏推荐
- Even if the image is missing in a large area, it can also be repaired realistically. The new model CM-GAN takes into account the global structure and texture details
- SetoolKit使用指南
- 网络协议及相关技术详解
- 技能大赛训练题:交换机的远程管理
- 模拟量差分和单端(iou计算方法)
- 代码随想录笔记_哈希_454四数相加II
- 基于改进YOLOv5的轻量化航空目标检测方法
- 技能大赛训练题:域用户和组织单元的创建
- 已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)
- 尚硅谷-JVM-内存和垃圾回收篇(P1~P203)
猜你喜欢

纸质说明书秒变3D动画,斯坦福大学吴佳俊最新研究,入选ECCV 2022

ECCV 2022 | Robotic Interaction Perception and Object Manipulation

A detailed explanation of the usage of Async and Await in C#

How IDEA runs web programs

ECCV2022: Recursion on Transformer without adding parameters and less computation!

AWS实现定时任务-Lambda+EventBridge

Open Inventor 10.12 重大改进--和谐版

CLion用于STM32开发

使用NVM进行node版本切换管理

已解决(pymysqL连接数据库报错)pymysqL.err.ProgrammingError: (1146,“Table ‘test.students‘ doesn‘t exist“)
随机推荐
1小时直播招募令:行业大咖干货分享,企业报名开启丨量子位·视点
推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】
对数字化时代的企业来说,数据治理难做,但应该去做
全局平均池化层替代全连接层(最大池化和平均池化的区别)
The operator,
最新完整代码:使用word2vec预训练模型进行增量训练(两种保存方式对应的两种加载方式)适用gensim各种版本
Shell项目实战1.系统性能分析
Samba 远程命令执行漏洞(CVE-2017-7494)
Controller层代码这么写,简洁又优雅!
How IDEA runs web programs
技能大赛训练题:登录安全加固
C#高级--委托
C语言基础练(九九乘法表)与打印不同星号图案
Introduction to the PartImageNet Semantic Part Segmentation dataset
The importance of strategic offensive capability is much higher than strategic defensive capability
Selenium IDE for Selenium Automation Testing
Shell脚本经典案例:文件的备份
C#控件ListView用法
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
An article makes it clear!What is the difference and connection between database and data warehouse?