package cn.mldn.lxh.Dao.Impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import cn.mldn.lxh.Dao.userDAO;
import cn.mldn.lxh.Dao.dbcon.User;
import cn.mldn.lxh.Dao.dbcon.dbConnection;
public class userDAOImpl implements userDAO{
public void insert(User user) throws Exception {
System.out.println("-----insert----");
dbConnection conn=new dbConnection();
PreparedStatement pstmt=null;
String sql="insert into user_information(name,password,age,email) values(?,?,?,?)";
System.out.println(sql);
System.out.println(user.getUserName());
try{
System.out.println("start insert");
pstmt=conn.getConnection().prepareStatement(sql);
System.out.println(sql);
pstmt.setString(1, user.getUserName());
System.out.println("insert beginning");
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getEmail());
pstmt.executeUpdate();
System.out.println("insert implement....");
pstmt.close();
}
catch(Exception e)
{
System.out.println("insert fail");
e.printStackTrace();
throw new Exception("操作中出现错误");
}
finally{
conn.close();
}
}
public void update(User user) throws Exception {
dbConnection conn=null;
PreparedStatement pstmt=null;
String sql="update user_information set name=?,password=?,age=?,email=? where id=?";
try{
pstmt=conn.getConnection().prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
pstmt.setInt(3, user.getAge());
pstmt.setString(4, user.getEmail());
pstmt.setInt(5, user.getId());
pstmt.executeUpdate();
pstmt.close();
}
catch(Exception e)
{
throw new Exception("操作中出现错误");
}
finally{
conn.close();
}
}
public void delete(int id) throws Exception {
dbConnection conn=null;
PreparedStatement pstmt=null;
String sql="delete from user_information where id=?";
try{
pstmt=conn.getConnection().prepareStatement(sql);
pstmt.setInt(1, id);
pstmt.executeUpdate();
pstmt.close();
}
catch(Exception e)
{
throw new Exception("操作中出现错误");
}
finally{
conn.close();
}
}
public User queryById(int id) throws Exception {
dbConnection conn=null;
User user=null;
PreparedStatement pstmt=null;
String sql="select * from user_information where id=?";
try{
pstmt=conn.getConnection().prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs=pstmt.executeQuery();
if(rs.next())
{
user=new User();
user.setUserName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
}
pstmt.close();
}
catch(Exception e)
{
throw new Exception("操作中出现错误");
}
finally{
conn.close();
}
return user;
}
public List queryAll() throws Exception {
List all=null;
dbConnection conn=null;
String sql="select * from user_information";
PreparedStatement pstmt=null;
try{
conn=new dbConnection();
pstmt=conn.getConnection().prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next())
{
User user=new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
all.add(user);
}
}catch(Exception e){}
finally{conn.close();}
return all;
}
public List queryByLike(String cond) throws Exception {
List all=new ArrayList();
dbConnection conn=null;
PreparedStatement pstmt=null;
String sql="select * from user_information where name like ? or remark like ?";
try{
conn=new dbConnection();
pstmt=conn.getConnection().prepareStatement(sql);
pstmt.setString(1, cond);
pstmt.setString(2, cond);
ResultSet rs=pstmt.executeQuery();
while(rs.next())
{
User user=new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
all.add(user);
}
}catch(Exception e){}
finally{conn.close();}
return all;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
DAO模式数据库操作源代码

共32个文件
java:10个
class:10个
jsp:7个


温馨提示
本例子是自己做的,,有很强的可读性。。代码使用率高。是用DAO模式设计的, 实现了对数据库的添加、删除、查询、更新等操作。。不懂的可以联系我:qq:420804832
资源推荐
资源详情
资源评论















收起资源包目录























































共 32 条
- 1
资源评论

- #完美解决问题
- #运行顺畅
- #内容详尽
- #全网独家
- #注释完整
- tangmgp2013-10-10很有用,源码值得参考。
- peng0312013-06-18看错了, 没注意到标Java, 浪费了一些很多分
- vae-cjyz2012-05-15东西不错,有用。 只是没有附带答案,有点遗憾
- blow_hot_and_cold2015-04-29看过,有一定参考价值.

风/xin云
- 粉丝: 52
- 资源: 9
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- Tipszotero搜索引擎的设置,更好地抓取论文信息
- ,,MD500E源码和代码解析文档 代码包含了同步机FOC控制算法、电阻、电感、磁链、反电动势、死区补偿、过调制限制、弱磁等算法,支持无感和有感,亲自带电机运行过 ,关键词:MD500E源码; 代
- 基于 Java AIO 和 T-IO: 利用 Java 异步 I/O 和 T-IO 提供高效性能 支持常用的 Spring-Boot 注解,但不使用 Spring 的 IOC 和 AOP
- PyTorch开源机器学习库简介与入门教程-适用于AI开发
- Nova for mac 代码编辑工具
- ,,FOC 无感 混合磁链观测器 电机控制 代码 PMSM MiniDD(直驱)电机变频无感程序,包含偏心,重量,共振等感知算法,所有算法都不基于库函数,MCU底层配置完全手写 ,核心关键词:FO
- 5G+AI+物联网智慧医院信息化顶层设计解决方案 (1).ppt
- 大数据湖总体规划及一体化运营管理建设方案.ppt
- 大数据集群治理与数据治理解决方案.ppt
- ,,流水线贴膜机完成项目程序,包含PLC程序和触摸屏程序,程序内 包含上下气缸控制,夹紧气缸控制,输送带电机控制,贴膜伺服控制,旋转电机控制等类容,非常适合学习简单控制工艺及运动控制初学者学习,该程序
- Dynatrace资料视频,介绍如何使用Dynatrace
- 大数据平台应用功能蓝图、大数据平台数据治理解决方案.ppt
- 大数据平台数据治理体系与大数据架构技术方案.ppt
- 大数据平台应用功能蓝图与数据治理解决方案.ppt
- ,,国内新能源汽车巨头某车型电机控制器软件源代码 ,基于TI 28x平台,含核心算法,全部开源,注释详细,程序规范,包含永磁同步电机FOC矢量控制算法、坡起辅助 、怠速蠕行、刹车油门扭矩协调、缺相诊断
- 大数据治理平台总体架构、技术架构、功能架构及数据应用解决方案.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
