JDBC insert delete update(DML)

思路感悟

在这里插入图片描述

原始数据1

在这里插入图片描述

class JDBCInsert

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCInsert {
    public static void main(String[] args)  {
        Connection connection = null;
        Statement statement = null;
        //第一步:注册驱动
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //第二步:创建连接
            String url="jdbc:mysql://127.0.0.1:3306/mobilemallsystem";
            String user="root";
            String password="123456";
            connection = DriverManager.getConnection(url, user, password);
            //第三步:获取操作SQL语句的对象
            statement=connection.createStatement();
            //第四步:执行SQL语句
            String sql="insert into usermessage(uname,upassword) values(121,121121),(122,122122)";
            int i = statement.executeUpdate(sql);
            //i为更新的表中的数据的条数
            if(i==2){
                System.out.println("数据插入成功");
            }
            //第五步:处理查询的结果集
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //第六步:释放资源
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

插入数据之后

在这里插入图片描述

原始数据2

在这里插入图片描述

JDBCDelete

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCDelete {
    public static void main(String[] args)  {
        Connection connection = null;
        Statement statement = null;
        //第一步:注册驱动
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //第二步:创建连接
            String url="jdbc:mysql://127.0.0.1:3306/mobilemallsystem";
            String user="root";
            String password="123456";
            connection = DriverManager.getConnection(url, user, password);
            //第三步:获取操作SQL语句的对象
            statement=connection.createStatement();
            //第四步:执行SQL语句
            String sql="delete from usermessage where uid=25";
            int i = statement.executeUpdate(sql);
            //i为更新的表中的数据的条数
            if(i==1){
                System.out.println("数据删除成功");
            }
            //第五步:处理查询的结果集
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //第六步:释放资源
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

删除数据之后

在这里插入图片描述

原始数据

在这里插入图片描述

class JDBCUpdate

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUpdate {
    public static void main(String[] args)  {
        Connection connection = null;
        Statement statement = null;
        //第一步:注册驱动
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            //第二步:创建连接
            String url="jdbc:mysql://127.0.0.1:3306/mobilemallsystem";
            String user="root";
            String password="123456";
            connection = DriverManager.getConnection(url, user, password);
            //第三步:获取操作SQL语句的对象
            statement=connection.createStatement();
            //第四步:执行SQL语句
            String sql="update usermessage set uname='123456789',upassword='123456789' where uid=24";
            int i = statement.executeUpdate(sql);
            //i为更新的表中的数据的条数
            if(i==1){
                System.out.println("数据更新成功");
            }
            //第五步:处理查询的结果集
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //第六步:释放资源
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}

更新数据之后

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值