[Java]使用java进行JDBC编程

首先要从中央仓库下载api(类似驱动程序),为了链接java和mysql

下载jar包,需要注意的是jar包的版本要和mysql保持一致

下面是新建文件夹lib,把jar包放进去,并添加为库

sql固定的情况下运行

java 复制代码
import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) throws SQLException {

        // 1.创建datasourse
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/qimo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //2.和数据库服务器建立连接,连接好了后,才能进行后续的请求+相应
        Connection connection = dataSource.getConnection();

        //3.构造sql
        String sql = "insert into employee values(6,700)";
//        String sql = "select * from employee";

        PreparedStatement statement = connection.prepareStatement(sql);

        //4.执行sql
         int n = statement.executeUpdate();

        //5.关闭连接
        statement.close();
        connection.close();
    }
}

让用户输入数据进行插入

这是我选择的非常简单的数据表

java 复制代码
import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    public static void main(String[] args) throws SQLException {

        // 1.创建datasourse
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/qimo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //2.和数据库服务器建立连接,连接好了后,才能进行后续的请求+相应
        Connection connection = dataSource.getConnection();

        //3.构造sql
        String sql = "insert into employee values(?,?)";
//        String sql = "select * from employee";
        PreparedStatement statement = connection.prepareStatement(sql);
        //这里的index是从1开始算的
        statement.setInt(1,18);
        statement.setInt(2,250);

        //4.执行sql
         int n = statement.executeUpdate();

        //5.关闭连接
        statement.close();
        connection.close();
    }
}

运行结果是这样:

相关推荐
m0_748873558 分钟前
C++与Rust交互编程
开发语言·c++·算法
今天和Aboo结婚了吗1 小时前
【Broker一重启消息没了:一次RabbitMQ非持久化+没开Confirm的血亏事故】
java·rabbitmq·messagequeue·bug排查
程序员陆业聪6 小时前
从 OpenClaw 到 Android:Harness Engineering 是怎么让 Agent 变得可用的
android
daidaidaiyu7 小时前
一文学习 工作流开发 BPMN、 Flowable
java
ZTLJQ7 小时前
序列化的艺术:Python JSON处理完全解析
开发语言·python·json
2401_891482177 小时前
多平台UI框架C++开发
开发语言·c++·算法
SuniaWang8 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题六:《Vue3 前端开发实战:打造企业级 RAG 问答界面》
java·前端·人工智能·spring boot·后端·spring·架构
sheji34168 小时前
【开题答辩全过程】以 基于springboot的扶贫系统为例,包含答辩的问题和答案
java·spring boot·后端
88号技师8 小时前
2026年3月中科院一区SCI-贝塞尔曲线优化算法Bezier curve-based optimization-附Matlab免费代码
开发语言·算法·matlab·优化算法
t198751288 小时前
三维点云最小二乘拟合MATLAB程序
开发语言·算法·matlab