当前位置:网站首页>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
边栏推荐
猜你喜欢
golang-gin - graceful restart
C#控件 ToolStripProgressBar 用法
【牛客刷题-SQL大厂面试真题】NO3.电商场景(某东商城)
关于MySQL主从复制的数据同步延迟问题
VU 非父子组件通信
An article makes it clear!What is the difference and connection between database and data warehouse?
使用NVM进行node版本切换管理
A detailed explanation of the usage of Async and Await in C#
技能大赛训练题:ftp 服务攻防与加固
SetoolKit使用指南
随机推荐
All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
C# control ListView usage
推荐系统-召回阶段-2013:DSSM(双塔模型)【Embedding(语义向量)召回】【微软】
pytorch gpu版本安装最新
golang-gin-优雅重启
C#使用NumericUpDown控件
Linux bash: redis-server: 未找到命令
自制的数据库安全攻防题,相关靶机自己制作
What should I do if selenium is reversed?
numpy矩阵和向量的保存与加载,以及使用保存的向量进行相似度计算
JSP response对象简介说明
龟速乘【模板】
selenium被反爬了怎么办?
六石编程学:不论是哪个功能,你觉得再没用,会用的人都离不了,所以至少要做到99%
技能大赛训练题:登录安全加固
Unity学习笔记 关于AVPro视频跳转功能(Seeking)的说明
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
C语言基础练(九九乘法表)与打印不同星号图案
最新完整代码:使用word2vec预训练模型进行增量训练(两种保存方式对应的两种加载方式)适用gensim各种版本
AI cocoa AI frontier introduction (7.31)