Java - JDBC编程简单实现MySQL数据库的增删改查

简单实现MySQL数据库的增删改查

一 , 说明

Java的JDBC编程其实就是一种套路模板,并不会很难,多上手写几遍一般就会了!
而JDBC 编程的核心是 sql 子句 。

二 , 实现步骤及需要的资源配置

  • 导包的实现步骤~~
      1. 首先创建一个项目
      1. 新建一个文件夹
      1. 导入jar包
      1. 右击添加(APP…)就可以使用了
  • 代码的实现步骤~~
      1. 创建数据源
      1. 连接数据库
      1. 构造sql语句
      1. 执行sql语句
      1. 释放资源

三, 具体实现

  • 下载jar包

首先打开游览器搜索MAVEN 或者直接点击打开 MAVEN
(这里可能打开会有检测是否为真人操作,直接跟着提示做就行)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 导包

在这里插入图片描述

  • 代码
// 添加数据
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class JDBCSafetyInsert {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        // 多态 - 向上转型
        DataSource dataSource = new MysqlDataSource();
        // 多态 - 向下转型
        // 设置数据库地址
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java107?characterEncoding=utf8&useSSL=false");
        // 设置用户名
        ((MysqlDataSource) dataSource).setUser("root");
        // 设置用户密码
        ((MysqlDataSource) dataSource).setPassword("123456789");
        // 2. 连接数据库
        Connection connection = dataSource.getConnection();
        // 2.5 控制台输入用户信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入ID:");
        int ID = sc.nextInt();
        System.out.println("请输入name:");
        String name = sc.next();
        // 3. 构造sql语句 - 这种写法可以防止注入式攻击
        String sql = "insert into student values(?,?)";
        // 预编译sql
        PreparedStatement statement = connection.prepareStatement(sql);
        // 转换sql语句
        statement.setInt(1,ID);
        statement.setString(2,name);
        System.out.println("statement: " + statement);
        // 4. 执行sql语句
        int ret = statement.executeUpdate();
        System.out.println("影响行数ret: " + ret);
        // 5. 释放资源
        statement.close();
        connection.close();
    }
}
// 删除数据
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDelete {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        DataSource datasource = new MysqlDataSource();
        // 设置数据库地址
        ((MysqlDataSource)datasource).setURL("jdbc:mysql://127.0.0.1:3306/java107?characterEncoding=utf8&useSSL=false");
        // 设置用户名
        ((MysqlDataSource)datasource).setUser("root");
        // 设置密码
        ((MysqlDataSource)datasource).setPassword("123456789");

        // 2. 连接数据库
        Connection connection = datasource.getConnection();

        // 2.5 控制台提醒
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除信息的id:");
        int ID = sc.nextInt();

        // 3. 构造sql语句
        String sql = "delete from  student where ID<=>?";
        // 预编译sql
        PreparedStatement statement = connection.prepareStatement(sql);
        // 转换sql
        statement.setInt(1,ID);
        System.out.println("statement: " + statement);

        // 4. 执行sql语句
        int ret = statement.executeUpdate();
        System.out.println("影响行数: " + ret);

        // 5. 释放资源
        statement.close();
        connection.close();
    }
}
// 修改数据
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

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

public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        // 创建连接地址
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java107?characterEncoding=utf8&useSSL=false");
        // 创建用户名
        ((MysqlDataSource)dataSource).setUser("root");
        // 创建登录密码
        ((MysqlDataSource)dataSource).setPassword("123456789");

        // 2. 连接数据库
        Connection connection = dataSource.getConnection();

        // 2.5 控制台提示
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入ID:");
        int ID = sc.nextInt();
        System.out.println("请输入name:");
        String name = sc.next();

        // 3. 构造sql语句
        String sql = "update student set name=? where ID=?";
        // 预编译
        PreparedStatement statement = connection.prepareStatement(sql);
        // 转换sql
        statement.setString(1,name);
        statement.setInt(2,ID);
        System.out.println("statement: " + statement);

        // 4. 执行sql语句
        // 增,删,改
        int ret = statement.executeUpdate();
        // 查询
        //ResultSet resultSet = statement.executeQuery();
        System.out.println("影响行数: " + ret);

        // 5. 释放资源
        statement.close();
        connection.close();
    }
}
// 查找数据
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

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

public class JDBCSelect {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        DataSource datasource = new MysqlDataSource();
        // 设置来连接地址
        ((MysqlDataSource)datasource).setURL("jdbc:mysql://127.0.0.1:3306/java107?characterEncoding=utf8&useSSL=false");
        // 设置用户名
        ((MysqlDataSource)datasource).setUser("root");
        // 设置用户密码
        ((MysqlDataSource)datasource).setPassword("123456789");

        // 2. 连接数据库
        Connection connection = datasource.getConnection();

        // 3. 构造sql
        String sql = "select * from student";
        // 预编译
        PreparedStatement statement = connection.prepareStatement(sql);

        // 4. 执行sql
        // 获取一张临时表
        ResultSet resultSet = statement.executeQuery();
        // 构建表的模型
        // 这里需要根据自己所创建的表来实现控制台所展示的样子 这里所表示的是一个表中有(ID,name)两个字段
        while(resultSet.next()) {
            int ID = resultSet.getInt("ID");
            String name = resultSet.getString("name");
            System.out.println("----------------------");
            System.out.println("| " + "ID: " + ID + " | " + "name: " + name + " |");
        }
        System.out.println("----------------------");
        // 5. 释放数据
        resultSet.close();
        connection.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值