表的基础操作(No.14)

本文介绍了一个使用Java实现的员工管理系统,该系统通过MySQL数据库进行数据存储,提供了员工信息增删改查等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package employee;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import employee.Work;

public class Employee implements Work {

private static Connection conn;

private PreparedStatement pstmt;

private ResultSet rs;

static {

try {

Class.forName("com.mysql.jdbc.Driver");

conn = DriverManager

.getConnection("jdbc:mysql://localhost:3306/work?user=root&password=like&useUnicode=true&characterEncoding=UTF-8");

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

}

@Override

public boolean add(String name) {

Boolean flag = false;

String sql = "insert into place(id,pName) values(null,?)";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setString(index++, name);

int i = pstmt.executeUpdate();

if (i > 0) {

flag = true;

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return flag;

}

@Override

public List check(String name) {

List allentities = new ArrayList();

String sql = "select * from employee as e join place as p on e.eId = p.id where pName = ?";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setString(index++, name);

rs = pstmt.executeQuery();

if (rs.next()) {

System.out

.println(rs.getInt("id") + "\t" + rs.getString("eName")

+ "\t" + rs.getString("eSex") + "\t"

+ rs.getInt("eAge") + "\t"

+ rs.getLong("eMoney") + "\t"

+ rs.getLong("eMoneyout"));

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return allentities;

}

@Override

public List avg(String name) {

List allentities = new ArrayList();

String sql = "select avg(eMoney+eMoneyout) from employee as e join place as p on e.eId = p.id where pName = ?";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setString(index++, name);

rs = pstmt.executeQuery();

if (rs.next()) {

System.out.println(rs.getLong("avg(eMoney+eMoneyout)"));

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return allentities;

}

@Override

public List count(String name) {

List allentities = new ArrayList();

String sql = " select avg(eMoney+eMoneyout)*count(e.id) from employee as e join place as p on e.eId = p.id where pName = ?";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setString(index++, name);

rs = pstmt.executeQuery();

if (rs.next()) {

System.out.println(rs

.getLong("avg(eMoney+eMoneyout)*count(e.id)"));

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return allentities;

}

@Override

public List check1(String name) {

List allentities = new ArrayList();

String sql = "select * from employee where eName = ?";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setString(index++, name);

rs = pstmt.executeQuery();

if (rs.next()) {

System.out

.println(rs.getInt("id") + "\t" + rs.getString("eName")

+ "\t" + rs.getString("eSex") + "\t"

+ rs.getInt("eAge") + "\t"

+ rs.getLong("eMoney") + "\t"

+ rs.getLong("eMoneyout"));

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return allentities;

}

@Override

public boolean change(int eMoney, String name) {

boolean flag = false;

String sql = "update employee set eMoney = ? where eName = ?";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setLong(index++, eMoney);

pstmt.setString(index++, name);

int i = pstmt.executeUpdate();

if (i > 0) {

flag = true;

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return flag;

}

@Override

public boolean delete(int id) {

boolean flag = false;

String sql = "delete from employee where id = ?";

try {

pstmt = conn.prepareStatement(sql);

int index = 1;

pstmt.setInt(index++, id);

int i = pstmt.executeUpdate();

if (i > 0) {

flag = true;

}

} catch (SQLException e) {

e.printStackTrace();

}

if (pstmt != null) {

try {

pstmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return flag;

}

public static void main(String[] args) {

Work st = new Employee();

// st.add("美女部");

// st.check("学习部");

// st.avg("体育部");

// st.count("学习部");

// st.check1("李四");

// st.change(40000, "李四");

// st.delete(2);

}

}

///////////////////////////////////////////////////////////////////Work.java代码

package employee;

import java.util.List;

public interface Work {

List check(String name);

List avg(String name);

List count(String name);

List check1(String name);

boolean change(int eMoney, String name);

boolean delete(int id);

boolean add(String name);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值