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