当前位置:网站首页>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
边栏推荐
- [Niu Ke brush questions - SQL big factory interview questions] NO3. E-commerce scene (some east mall)
- 网络协议及相关技术详解
- 技能大赛训练题:交换机虚拟化练习
- Invalid bound statement (not found)出现的原因和解决方法
- Text similarity calculation (Chinese and English) detailed explanation of actual combat
- numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
- 技能大赛训练题:交换机的远程管理
- Open Inventor 10.12 重大改进--和谐版
- uniapp微信小程序引用标准版交易组件
- MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!
猜你喜欢
网络协议及相关技术详解
C#使用NumericUpDown控件
Detailed explanation of network protocols and related technologies
对数字化时代的企业来说,数据治理难做,但应该去做
AWS implements scheduled tasks - Lambda+EventBridge
使用NVM进行node版本切换管理
How IDEA runs web programs
4.爬虫之Scrapy框架2数据解析&配置参数&数据持久化&提高Scrapy效率
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
Spark Learning: Add Custom Optimization Rules for Spark Sql
随机推荐
Reasons and solutions for Invalid bound statement (not found)
推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】
MySQL has played to such a degree, no wonder the big manufacturers are rushing to ask for it!
页面整屏滚动效果
ADS与C#通信
自制的数据库安全攻防题,相关靶机自己制作
C# control StatusStrip use
ECCV2022: Recursion on Transformer without adding parameters and less computation!
C#控件 ToolStripProgressBar 用法
What should I do if selenium is reversed?
动作捕捉系统用于柔性机械臂的末端定位控制
A detailed explanation of the usage of Async and Await in C#
Introduction to the PartImageNet Semantic Part Segmentation dataset
C#获得网卡信息 NetworkInterface IPInterfaceProperties
AWS implements scheduled tasks - Lambda+EventBridge
C# control ListView usage
endnote引用
C#控件StatusStrip使用
csdn发文助手问题
LeetCode旋转数组