文章目录
一、IDEA连接MySQL数据库(一)首先新建普通Java项目(二)连接数据库1、点击右侧DataBase2、点击加号,找到MySQL,添加数据库3、输入用户名和密码,点击**Test Connection**4、显示连接成功,点击Apply,OK5、发现没有数据库,点击**1 of 7**,选择**All schemas** 二、使用数据库的数据(一)新建Java类 Test(二)下载MySQL驱动Jar包方法一:官网下载方法二:百度盘 (三)返回IDEA,新建文件夹lib(四)非常重要(添加到库)右击加入的Jar包,选择**Add as Library(添加到库)** (五)编写Test类boss表结构如下(自己表结构要修改代码) (六)运行主函数,得到数据 三、更多好玩好学的内容请关注
一、IDEA连接MySQL数据库
(一)首先新建普通Java项目

(二)连接数据库
1、点击右侧DataBase

2、点击加号,找到MySQL,添加数据库

3、输入用户名和密码,点击Test Connection

4、显示连接成功,点击Apply,OK


5、发现没有数据库,点击1 of 7,选择All schemas

即可显示全部的数据库

二、使用数据库的数据
(一)新建Java类 Test

(二)下载MySQL驱动Jar包
方法一:官网下载
官网地址
选择 Plantfrom Independent 版本,下载第二个。

点击不登录,只下载

方法二:百度盘
https://pan.baidu.com/s/1yTS6XeTWJ7hm2Z7zPeV_Iw?pwd=qw44
提取码:qw44
(三)返回IDEA,新建文件夹lib

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

(四)非常重要(添加到库)
右击加入的Jar包,选择Add as Library(添加到库)

(五)编写Test类
import java.sql.*;public class Test { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); // 使用我自己的数据库 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语句,使用我自己的test数据库下的 boss 表 String sql = "SELECT * FROM boss"; 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("age = " + resultSet.getObject("age")); } resultSet.close(); statement.close(); connection.close(); }}
boss表结构如下(自己表结构要修改代码)

(六)运行主函数,得到数据

三、更多好玩好学的内容请关注
宫众耗:快乐箱箱