package implement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import bean.UserInfo;
public class UserInfoImpl {
private Connection conn = null;
public UserInfoImpl(Connection conn){
this.conn = conn;
}
public int insert(UserInfo userinfo) {
int count = 0;
String sql = "insert into student(name,password,sex,age,addr,email)values(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userinfo.getName());
ps.setString(2, userinfo.getPassword());
ps.setString(3, userinfo.getSex());
ps.setInt(4, userinfo.getAge());
ps.setString(5, userinfo.getAddr());
ps.setString(6, userinfo.getEmail());
count = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return count;
}
public int delete(String idArr) {
String sql = "delete from student where id in (" + idArr + ")";
PreparedStatement ps = null;
ResultSet rs = null;
int rowNum = 0;
try {
ps = conn.prepareStatement(sql);
rowNum = ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return rowNum;
}
public boolean deleteRecord(int cid) {
String sql = "delete from student where id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
int rowNum = 0;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, cid);
rowNum = ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
if(rowNum > 0){
return true;
}else{
return false;
}
}
public int update(UserInfo userinfo) {
int count = 0;
String sql = "update student set name = ?, password = ?, sex = ?, age = ?, addr = ?, email = ? where id = ?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userinfo.getName());
ps.setString(2, userinfo.getPassword());
ps.setString(3, userinfo.getSex());
ps.setInt(4, userinfo.getAge());
ps.setString(5, userinfo.getAddr());
ps.setString(6, userinfo.getEmail());
ps.setInt(7, userinfo.getId());
count = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return count;
}
public UserInfo query(String uname,String pwd) {
String sql = "select * from student where name=? and password=?";
UserInfo userinfo = new UserInfo();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
ps.setString(2, pwd);
rs = ps.executeQuery();
while(rs.next()){
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return userinfo;
}
public UserInfo queryById(int id) {
String sql = "select * from student where id = ?";
UserInfo userinfo = new UserInfo();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while(rs.next()){
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return userinfo;
}
public boolean checkUser(String uname){
boolean flag = false;
String sql = "select * from student where name = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery();
while (rs.next()) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return flag;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List getSearch(String sql) {
List list = new ArrayList();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
UserInfo userinfo = new UserInfo();
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
list.add(userinfo);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}
public ArrayList<UserInfo> queryPaging(int start,int size){
String sql = "select * from student limit ?,?";
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<UserInfo> al = new ArrayList<UserInfo>();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, size);
rs = ps.executeQuery();
while (rs.next()) {
UserInfo userinfo = new UserInfo();
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
al.add(userinfo);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return al;
}
public int queryRecord() {
String sql = "select count(*) from student";
PreparedStatement ps = null;
ResultSet rs = null;
int c = 0;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
c = rs.getInt(1);
}
}catch (Exception

w_mamba
- 粉丝: 0
最新资源
- update9-20250731.5.209.slice.img.7z.003
- update9-20250731.5.209.slice.img.7z.004
- 单相交错图腾柱PFC双闭环PI控制仿真实现与优化技巧
- update9-20250731.5.209.slice.img.7z.005
- 基于MATLAB的电流跟踪PWM控制技术:三相逆变器系统设计与仿真实现
- Spring Data JPA实现分页查询功能的完整示例
- 基于TMS320F28335的DSP移相程序:清晰逻辑,注释详尽,专业处理方波信号,开关频率达225kHz,支持后两路移相输出
- 自动驾驶Lattice规划算法详解:轨迹采样、评估与碰撞检测的Matlab和C++实现
- 电力电子领域三相四桥臂逆变器接非线性与不平衡负载的多准PR并联控制研究
- 基于INGO-BiLSTM与改进北方苍鹰优化算法的电力功率负荷预测模型及其超参数优化
- 基于Python的考试管理系统(试题管理 自动阅卷)
- STM32低成本简化版MD500E变频器与永磁同步电机控制算法核心代码解析
- 基于正负序分离技术的三电平NPC整流器不平衡电压控制模型预测与仿真研究
- elasticsearch ik-8 分词器
- 直齿轮六自由度平移-扭转耦合非线性动力学程序:时变压力角与齿侧间隙的影响分析 深度版
- Carsim与Simulink驾驶员在环实时仿真教程:cpar文件与联合仿真文件解析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0