package cn.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class DocManage {
// 空构造方法
public DocManage() {
}
// 添加兽医
public int addPetdoc(UserBean user) {
String sql = "INSERT INTO petdoctor(docname,docmajor,docphone,docadress) VALUES (?,?,?,?)";
int i = 0;
try {
ConMysql.prepareConnection();
ConMysql.con.setAutoCommit(false);
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setString(1, user.getDocname());
ConMysql.ps.setString(2, user.getDocmajor());
ConMysql.ps.setString(3, user.getDocphone());
ConMysql.ps.setString(4, user.getDocadress());
i = ConMysql.ps.executeUpdate();
ConMysql.con.commit();
} catch (SQLException e) {
ConMysql.rollback();
e.printStackTrace();
} finally {
ConMysql.close();
}
return i;
}
// 删除兽医
public int delPetdocForId(UserBean user) {
String sql = "DELETE FROM petdoctor WHERE id=?";
int i = 0;
try {
ConMysql.prepareConnection();
ConMysql.con.setAutoCommit(false);
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setInt(1, user.getId());
i = ConMysql.ps.executeUpdate();
ConMysql.con.commit();
} catch (SQLException e) {
ConMysql.rollback();
e.printStackTrace();
} finally {
ConMysql.close();
}
return i;
}
// 通过id删除兽医
public int delPetdocForId(int id) {
String sql = "DELETE FROM petdoctor WHERE id=?";
int i = 0;
try {
ConMysql.prepareConnection();
ConMysql.con.setAutoCommit(false);
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setInt(1, id);
i = ConMysql.ps.executeUpdate();
ConMysql.con.commit();
} catch (SQLException e) {
ConMysql.rollback();
e.printStackTrace();
} finally {
ConMysql.close();
}
return i;
}
// 更新兽医
public int updatePetdocForId(UserBean user) {
String sql = "UPDATE petdoctor SET docname=?, docmajor=?,docphone=?,docadress=? WHERE id=?";
int i = 0;
try {
ConMysql.prepareConnection();
ConMysql.con.setAutoCommit(false);
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setString(1, user.getDocname());
ConMysql.ps.setString(2, user.getDocmajor());
ConMysql.ps.setString(3, user.getDocphone());
ConMysql.ps.setString(4, user.getDocadress());
ConMysql.ps.setInt(5, user.getId());
i = ConMysql.ps.executeUpdate();
ConMysql.con.commit();
} catch (SQLException e) {
ConMysql.rollback();
e.printStackTrace();
} finally {
ConMysql.close();
}
return i;
}
// 获取所有兽医
public List<UserBean> getAllPetdoc() {
String sql = "SELECT * FROM petdoctor";
List<UserBean> users = new ArrayList<UserBean>();
try {
ConMysql.prepareConnection();
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ResultSet rs = ConMysql.ps.executeQuery();
while (rs.next()) {
UserBean user = new UserBean();
user.setId(rs.getInt("id"));
user.setDocname(rs.getString("docname"));
user.setDocmajor(rs.getString("docmajor"));
user.setDocphone(rs.getString("docphone"));
user.setDocadress(rs.getString("docadress"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConMysql.close();
}
return users;
}
// 通过兽医名获取兽医
public List<UserBean> getPetdocForName(UserBean use) {
String sql = "SELECT * FROM petdoctor WHERE docname=?";
List<UserBean> users = new ArrayList<UserBean>();
try {
ConMysql.prepareConnection();
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setString(1, use.getDocname());
ResultSet rs = ConMysql.ps.executeQuery();
while (rs.next()) {
UserBean user = new UserBean();
user.setId(rs.getInt("id"));
user.setDocname(rs.getString("docname"));
user.setDocmajor(rs.getString("docmajor"));
user.setDocphone(rs.getString("docphone"));
user.setDocadress(rs.getString("docadress"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConMysql.close();
}
return users;
}
// 通过兽医专业获取兽医
public List<UserBean> getPetdocForMajor(UserBean use) {
String sql = "SELECT * FROM petdoctor WHERE docmajor=?";
List<UserBean> users = new ArrayList<UserBean>();
try {
ConMysql.prepareConnection();
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setString(1, use.getDocmajor());
ResultSet rs = ConMysql.ps.executeQuery();
while (rs.next()) {
UserBean user = new UserBean();
user.setId(rs.getInt("id"));
user.setDocname(rs.getString("docname"));
user.setDocmajor(rs.getString("docmajor"));
user.setDocphone(rs.getString("docphone"));
user.setDocadress(rs.getString("docadress"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConMysql.close();
}
return users;
}
// 通过兽医专业获取兽医
public UserBean getPetdocForId(int id) {
String sql = "SELECT * FROM petdoctor WHERE id=?";
UserBean user = new UserBean();
try {
ConMysql.prepareConnection();
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setInt(1, id);
ResultSet rs = ConMysql.ps.executeQuery();
while (rs.next()) {
user.setId(rs.getInt("id"));
user.setDocname(rs.getString("docname"));
user.setDocmajor(rs.getString("docmajor"));
user.setDocphone(rs.getString("docphone"));
user.setDocadress(rs.getString("docadress"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConMysql.close();
}
return user;
}
// 通过兽医名获取id
public Integer getIdForName(UserBean user) {
String sql = "SELECT * FROM petdoctor WHERE docname=?";
Integer i = 0;
try {
ConMysql.prepareConnection();
ConMysql.ps = ConMysql.con.prepareStatement(sql);
ConMysql.ps.setString(1, user.getLoginname());
ResultSet rs = ConMysql.ps.executeQuery();
while (rs.next()) {
i = rs.getInt("id");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConMysql.close();
}
return i;
}
}
评论0