package com.maker.address.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.maker.address.entity.User;
import com.maker.address.util.DBUtil;
/**
* dao层处理对数据库的操作
* @author Administrator
*
*/
public class UserDao {
//新增用户
public boolean add(User user) {
// TODO Auto-generated method stub
String sql = "insert into user(no,name,phone,qq,email) values ("+user.getNo()+",'"+user.getName()+"','"+user.getPhone()+"','"+user.getQQ()+"','"+user.getEmail()+"')";
Connection conn = DBUtil.getConn();
Statement state = null;
int num = 0;
System.out.println(sql);
try {
state = conn.createStatement();
num = state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
return num > 0;
}
//查询所有用户
public ArrayList getAll() {
// TODO Auto-generated method stub
String sql = "select * from user";
Connection conn = DBUtil.getConn();
Statement state = null;
ArrayList<User> list = new ArrayList<>();
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while(rs.next()){
//结果集中有数据 代表查询出来有数据
//创建user对象,将数据存入到对象集合中
User user = new User();
user.setNo(rs.getInt("no"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setQQ(rs.getString("qq"));
user.setEmail(rs.getString("email"));
list.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(rs,state, conn);
}
return list;
}
//按照编号查询用户
public User queryByNo(int no) {
// TODO Auto-generated method stub
String sql = "select * from user where no="+no;
Connection conn = DBUtil.getConn();
Statement state = null;
User user = null;
ResultSet rs = null;
try {
state = conn.createStatement();
rs = state.executeQuery(sql);
while(rs.next()){
user = new User();
//结果集中有数据 代表查询出来有数据
//创建user对象,将数据存入到对象集合中
user.setNo(rs.getInt("no"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setQQ(rs.getString("qq"));
user.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(rs,state, conn);
}
return user;
}
//删除用户按照编号
public boolean remove(User user) {
// TODO Auto-generated method stub
String sql = "delete from user where no = '"+user.getNo()+"'";
Connection conn = DBUtil.getConn();
Statement state = null;
int num = 0;
try {
state = conn.createStatement();
num = state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
return num > 0;
}
//修改用户按照编号修改
public boolean updateUser(User user) {
// TODO Auto-generated method stub
String sql = "update user set name='"+user.getName()+"',phone='"+user.getPhone()+"',qq='"+user.getQQ()+"',email='"+user.getEmail()+"' where no = "+user.getNo();
Connection conn = DBUtil.getConn();
Statement state = null;
int num = 0;
try {
state = conn.createStatement();
num = state.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(state, conn);
}
return num > 0;
}
}
maker011
- 粉丝: 7498
- 资源: 81
最新资源
- 1732669747878.jpg
- 8613985ec49eb8f757ae6439e879bb2a_holmes_logindex
- 使用Spring Boot处理大文件上传有哪些技巧?
- GRE隧道是Openwrt系统的常用功能之一,在两台Openwrt设备之间建立GRE隧道,可通过两台设备在公网上建立一个虚拟局域网
- 机器学习领域中的Iris数据集探索与分析技术解析及建模流程
- 本科毕业设计-Java Web学院的实验课选课及实验室管理系统.zip
- MySQL在Linux安装和集群搭建-一主一备
- Python中检查文件是否存在的几种方法
- 02b97e6de561b351e3db753f4abedc7b.rar
- java JDK11版本安装包
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈