前言
开始链接前,请确保本机上安装的 idea 是 Ultimate 专业版,点我下载,JetBrains 旗下 Community 社区版本并未集成数据库开发工具,这一点我们从官网两个版本的下载介绍上也能看到

配置 MySQL
安装
一般来说,安装V5左右的版本就足够,版本号越大占用后台资源会更多,我们根据实际开发需求,选择合适的就好,这里演示 5.7 版本的安装过程(其他版本基本一致,按钮位置可能有所不同)
进入官方网站后,点击 Looking for previous GA versions 切换到早期版本

在线安装就看你的网速质量了,第二个是完整安装文件,直接下载就好(如果需要验证,登陆 Oracle 账号校验即可)

详细的安装教程请看这里,这里主要讲下配置过程
添加环境变量
右键【此电脑】->【属性】->【高级系统设置】->【环境变量】
添加MYSQL_HOME变量,变量值为电脑 MySQL 的安装位置

添加 Path 系统变量

检查配置
Windows+
R 打开命令行,键入
cmd 打开命令行,输入
1
| my sql -uroot -p //root表示用户名,-p表示输入用户密码
|
输入安装 MySQL 时的账户密码,出现图示信息则配置正确

MysQL服务状态
以管理员身份打开命令行,分别输入以下命令
开启
关闭

在idea Ultimate中建立连接
引入 Drivers 驱动
建立项目后,添加 MysQL数据源

找到驱动路径

引入外部库中

成功后,项目结构中也会显示

添加表
创建 schema 架构


创建 Table 表

添加属性

写入数据信息
方式1:可视化,图形界面操作

方式2:sql 语句添加

执行结果

测试类
请根据注释位置,修改配置信息后再 Execute 执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| import java.sql.*;
public class Test { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/your schema"; static final String USER = "your sql account"; static final String PASS = "your sql password";
public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ Class.forName(JDBC_DRIVER);
System.out.println("数据库建立链接中..."); conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("正在实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "SELECT * FROM information"; ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String age = rs.getString("age");
System.out.print("ID: " + id); System.out.print(", 姓名: " + name); System.out.print(", 年龄: " + age); System.out.println(""); } rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ se.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ } try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("数据库链接释放!"); } }
|
测试类执行结果

参考资料: