使用JDBC连接MySQL数据库并执行更新操作的示例解析

本文通过一个简单的Java示例,展示了如何使用JDBC连接MySQL数据库,并执行一条更新语句。示例详细说明了驱动注册、数据库连接、SQL执行以及资源释放的完整流程,适合初学者快速上手Java数据库操作。

create table employee ( id int unsigned auto_increment comment 'id' primary key, username varchar(20) not null comment '用户名', password varchar(32) default '123456' null comment '密码', name varchar(10) not null comment '姓名', gender tinyint unsigned not null comment '性别,1:男,2:女', phone varchar(11) not null comment '手机号', job tinyint unsigned null comment '职位,1:班主任;2:讲师;3:学工主管;4:教研主管;5:咨询师', entry_date date null comment '入职时间', image varchar(255) null comment '头像地址', create_time datetime null comment '创建时间', update_time datetime null comment '更新时间', constraint phone unique (phone), constraint username unique (username) ) comment '员工表'; 
INSERT INTO employee (username, password, name, gender, phone, job, entry_date, image, create_time, update_time)
VALUES
('user01', '123456', '张三', 1, '13800000001', 1, '2022-01-10', 'http://example.com/image1.jpg', NOW(), NOW()),
('user02', '123456', '李四', 2, '13800000002', 2, '2021-12-15', 'http://example.com/image2.jpg', NOW(), NOW()),
('user03', '123456', '王五', 1, '13800000003', 3, '2020-05-20', 'http://example.com/image3.jpg', NOW(), NOW()),
('user04', '123456', '赵六', 2, '13800000004', 4, '2019-03-30', 'http://example.com/image4.jpg', NOW(), NOW()),
('user05', '123456', '钱七', 1, '13800000005', 5, '2022-06-10', 'http://example.com/image5.jpg', NOW(), NOW()),
('user06', '123456', '孙八', 1, '13800000006', 1, '2021-11-01', 'http://example.com/image6.jpg', NOW(), NOW()),
('user07', '123456', '周九', 2, '13800000007', 2, '2020-08-25', 'http://example.com/image7.jpg', NOW(), NOW()),
('user08', '123456', '吴十', 1, '13800000008', 3, '2019-07-12', 'http://example.com/image8.jpg', NOW(), NOW());
 <dependencies>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
    </dependencies>
package com.it;

import com.mysql.cj.jdbc.Driver;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcTest {
    @Test
    public void JdbcTest() throws Exception {
        //注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        // 连接数据库
        String url = "jdbc:mysql://localhost:3306/demo";
        String username = "root";
        String password = "root";
        Connection connection =  DriverManager.getConnection(url,username,password);
        //获取SQL语句执行对象
        Statement statement =  connection.createStatement();
        //执行数据库
        int i = statement.executeUpdate("update employee set username = 'admin' where id  = 14");
        System.out.println("----" + i);
        //释放资源
        statement.close();
        connection.close();
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容