package com.neusoft.dao;
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;
public class DeptDaoImpl implements DeptDao {
@Override
public void insertDept(Dept dept) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn=DbUtils.getConnection();
//3.操作数据库
String sql="insert into dept(deptno,dname,loc) values(?,?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, dept.getDeptno());
pstmt.setString(2, dept.getDname());
pstmt.setString(3,dept.getLoc());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
//4.关闭数据库
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
}
@Override
public void updateDept(Dept dept) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn=DbUtils.getConnection();
String sql="update dept set dname=?,loc=? where deptno=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, dept.getDname());
pstmt.setString(2, dept.getLoc());
pstmt.setInt(3, dept.getDeptno());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
//4.关闭数据库
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
}
@Override
public void deleteDept(int deptno) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn=DbUtils.getConnection();
String sql="delete from dept where deptno=?";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
//4.关闭数据库
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
}
@Override
public List<Dept> getDepts() {
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
List<Dept> depts=new ArrayList<Dept>();
try {
conn=DbUtils.getConnection();
//3.操作数据库
String sql="select * from dept";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next())
{
Dept dept=new Dept();
dept.setDeptno(rs.getInt("DEPTNO"));//
dept.setDname(rs.getString("DNAME"));
dept.setLoc(rs.getString("LOC"));
depts.add(dept);
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closeResultSet(rs);
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
return depts;
}
@Override
public Dept getDeptByDeptno(int deptno) {
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
Dept dept=new Dept();
try {
conn=DbUtils.getConnection();
//3.操作数据库
String sql="select * from dept where deptno=?";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
rs=pstmt.executeQuery();
if(rs.next())
{
dept.setDeptno(rs.getInt("DEPTNO"));
dept.setDname(rs.getString("DNAME"));
dept.setLoc(rs.getString("LOC"));
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
DbUtils.closeResultSet(rs);
DbUtils.closePreparedStatement(pstmt);
DbUtils.closeConnection(conn);
}
return dept;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
javaeemysql.rar (32个子文件)
javaeemysql
.project 1004B
.settings
.jsdtscope 491B
org.eclipse.wst.common.component 466B
org.eclipse.jdt.core.prefs 357B
org.eclipse.wst.common.project.facet.core.xml 296B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
com
neusoft
dao
DeptDaoImpl.java 4KB
DeptDao.java 224B
DbUtils.java 2KB
Dept.java 698B
controller
DeptServlet.java 3KB
service
DeptService.java 265B
DeptServiceImpl.java 973B
build
classes
com
neusoft
dao
DeptDao.class 368B
Dept.class 1KB
DeptDaoImpl.class 4KB
DbUtils.class 3KB
controller
DeptServlet.class 3KB
service
DeptService.class 380B
DeptServiceImpl.class 1KB
WebContent
WEB-INF
lib
annotations-api.jar 17KB
servlet-api.jar 194KB
ojdbc6.jar 2.01MB
mysql-connector-java-5.0.8-bin.jar 528KB
classes12.jar 1.53MB
dept
insert.jsp 817B
update.jsp 973B
main.jsp 1KB
index.jsp 402B
META-INF
MANIFEST.MF 36B
.classpath 576B
共 32 条
- 1
资源评论
- bj123062020-07-22能值5个C币都是高估,没什么东西
- crawlertinux2019-06-09一般般。。
- ray___w2019-05-24空的,里面没有东西
- 「已注销」2019-01-13还行吧......
- nuojin28862018-12-28还不错,。。。。
马衍硕
- 粉丝: 343
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功