JDBC实现写数据

JDBC实现写数据(INSERT、DELETE、UPDATE)

INSERT:

package com.imooc.jdbc.hrapp.command;

import com.imooc.jdbc.goodapp.common.DBUtils;

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

/**
 * @author CubeMonkey
 * @create 2020-10-11 10:03
 */
public class InsertCommand implements Command {
    @Override
    public void execute() {
        Scanner in = new Scanner(System.in);
        System.out.print("请输入员工编号:");
        int eno = in.nextInt();
        System.out.print("请输入员工姓名:");
        String ename = in.next();
        System.out.print("请输入员工薪资:");
        float salary = in.nextFloat();
        System.out.print("请输入隶属部门:");
        String dname = in.next();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtils.getConnection();
            String sql = "insert into employee(eno, ename, salary, dname) values(?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, eno);
            pstmt.setString(2, ename);
            pstmt.setFloat(3, salary);
            pstmt.setString(4, dname);
            int cnt = pstmt.executeUpdate();
            System.out.println("cnt:"+cnt);
            System.out.println(ename + "员工入职手续已办理");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtils.closeConnection(null, pstmt, conn);
        }
    }
}

DELETE

package com.imooc.jdbc.hrapp.command;

import com.imooc.jdbc.goodapp.common.DBUtils;

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

/**
 * 删除员工数据
 * @author CubeMonkey
 * @create 2020-10-11 10:21
 */
public class DeleteCommand implements Command {
    @Override
    public void execute() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入员工编号:");
        int eno = scanner.nextInt();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtils.getConnection();
            String sql = "delete from employee where eno = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, eno);

            int cnt = pstmt.executeUpdate();
            if (cnt == 1){
                System.out.println("员工离职手续已完成");
            }else{
                System.out.println("未找到" + eno + "编号员工数据");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.closeConnection(null, pstmt, conn);
        }


    }
}

UPDATE:

package com.imooc.jdbc.hrapp.command;

import com.imooc.jdbc.goodapp.common.DBUtils;

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

/**
 * 员工调薪
 * @author CubeMonkey
 * @create 2020-10-11 10:21
 */
public class UpdateCommand implements Command {
    @Override
    public void execute() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入员工编号:");
        int eno = scanner.nextInt();
        System.out.print("请输入员工薪资:");
        float salary = scanner.nextFloat();
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = DBUtils.getConnection();
            String sql = "update employee set salary = ? where eno = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setFloat(1, salary);
            pstmt.setInt(2, eno);

            int cnt = pstmt.executeUpdate();
            if (cnt == 1){
                System.out.println("员工薪资调整完毕");
            }else{
                System.out.println("未找到" + eno + "编号员工数据");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtils.closeConnection(null, pstmt, conn);
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rex·Lin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值