IDEA 实现 JDBC 操作 mysql

1. 首先准备要测试的数据库

-- 创建用来测试 jdbc 的表
CREATE TABLE IF NOT EXISTS jdbcTest(
	`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '测试主键id',
	`username` VARCHAR(30) NOT NULL COMMENT '用户名',
	`password` VARCHAR(30) NOT NULL COMMENT '密码',
	`email` VARCHAR(30) NOT NULL COMMENT '邮箱',
	`birthday` DATE NOT NULL COMMENT '生日',
	PRIMARY KEY (`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8;

-- 添加几条测试数据
INSERT INTO `test`.`jdbctest`(`id`, `username`, `password`, `email`, `birthday`) VALUES 
(1, '小明', '123456', '897161@126.com', '2000-02-09 21:15:55'),
(2, '小刚', '123456', '897161@126.com', '2000-02-09 21:15:55'),
(3, '小李', '123456', '897161@126.com', '2000-02-09 21:15:55')

2. 准备好 mysql-connector-java-5.1.47.jar 驱动

可以百度 Maven 仓库下载或去码云上下载我的仓库取其中的 jar 包

3. 创建一个普通的 java 项目,为其添加依赖

3.1 首先创建一个空项目
3.2 然后在该目录下创建 lib 文件夹,并将其 mysql 驱动放进去
3.3 将此 lib 文件夹添加到项目依赖中去
在这里插入图片描述
在这里插入图片描述
成功后的目录如下即可:
在这里插入图片描述

4. 编写测试代码操作 jdbc

package com.jdbc.test;

import java.sql.*;

/**
 * 第一个 jdbc 测试案例
 * @author: 南独酌酒 <211425401@126.com>
 * @date: 2020/9/21 21:29
 */
public class JDBCTestDemo {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet result = null;
        try {
            //1. 加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2. 提供数据库连接的信息和账号密码
            /**
             * useUnicode=true 支持中文编码
             * characterEncoding=utf8 设置字符集的编码格式
             * useSSL=true 使用安全的连接
             */
            String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true";
            String username = "root";
            String password = "123456";
            //3. 连接数据库
            connection = DriverManager.getConnection(url, username, password);
            //4. 创建执行 sql 的对象
            statement = connection.createStatement();
            //5. 执行 SQL、获取返回结果并查看
            String sql = "SELECT `id`,`username`,`password`,`email`,`birthday` FROM jdbcTest";
            result = statement.executeQuery(sql);
            while (result.next()) {
                System.err.println("id=" + result.getInt("id"));
                System.err.println("username=" + result.getString("username"));
                System.err.println("password=" + result.getString("password"));
                System.err.println("email=" + result.getString("email"));
                System.err.println("birthday=" + result.getDate("birthday"));
                System.err.println("======================");
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        } finally {
            //6. 释放连接
            try {
                result.close();
                statement.close();
                connection.close();
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }
}

5. 打印结果

在这里插入图片描述

6. 码云仓库地址

https://gitee.com/jian_bo_bai/technical_documents
在这里插入图片描述

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值