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;
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;
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;
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);
}
}
}