package cn.com.sise.dao.impl;
import cn.com.sise.dao.ContactDao;
import cn.com.sise.dto.Contact;
import cn.com.sise.utils.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class ContactDaoMySQLImpl implements ContactDao{
@Override
public void addContact(Contact contact) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = JDBCUtil.getConnection();
String sql = "INSERT INTO contact(id,name,gender,age,phone,email,qq,type) VALUES(?,?,?,?,?,?,?,?)";
//创建PrepareStatement
stmt = conn.prepareStatement(sql);
String id = UUID.randomUUID().toString().replace("-","");
//设置参数
stmt.setString(1,id);
stmt.setString(2,contact.getName());
stmt.setString(3,contact.getGender());
stmt.setInt(4,contact.getAge());
stmt.setString(5,contact.getPhone());
stmt.setString(6,contact.getEmail());
stmt.setString(7,contact.getQq());
stmt.setString(8,contact.getType());
//执行
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,stmt);
}
}
@Override
public void updateContact(Contact contact) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = JDBCUtil.getConnection();
String sql = "UPDATE contact SET name=?,gender=?,age=?,phone=?,email=?,qq=?,type=? WHERE id=?";
//创建PrepareStatement
stmt = conn.prepareStatement(sql);
String id = UUID.randomUUID().toString().replace("-","");
//设置参数
stmt.setString(1,contact.getName());
stmt.setString(2,contact.getGender());
stmt.setInt(3,contact.getAge());
stmt.setString(4,contact.getPhone());
stmt.setString(5,contact.getEmail());
stmt.setString(6,contact.getQq());
stmt.setString(7,contact.getType());
stmt.setString(8,contact.getId());
//执行
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,stmt);
}
}
@Override
public void deleteContact(String id) {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = JDBCUtil.getConnection();
String sql = "DELETE FROM contact WHERE id=?";
//创建PrepareStatement
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setString(1,id);
//执行
stmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,stmt);
}
}
@Override
public List<Contact> findAll() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
//获取连接
conn = JDBCUtil.getConnection();
String sql = "SELECT * FROM contact";
//创建PreparedStatement
stmt = conn.prepareStatement(sql);
//执行
rs = stmt.executeQuery();
List<Contact> list = new ArrayList<Contact>();
while(rs.next()){
Contact c = new Contact();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setAge(rs.getInt("age"));
c.setPhone(rs.getString("phone"));
c.setEmail(rs.getString("email"));
c.setQq(rs.getString("qq"));
c.setType(rs.getString("type"));
list.add(c);
}
return list;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
JDBCUtil.close(conn, stmt ,rs);
}
}
@Override
public Contact findById(String id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
String sql = "SELECT * FROM contact WHERE id=?";
//创建PrepareStatement
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setString(1,id);
//执行
rs = stmt.executeQuery();
Contact contact = null;
while (rs.next()){
contact = new Contact();
contact.setId(rs.getString("id"));
contact.setName(rs.getString("name"));
contact.setGender(rs.getString("gender"));
contact.setAge(rs.getInt("age"));
contact.setPhone(rs.getString("phone"));
contact.setEmail(rs.getString("email"));
contact.setQq(rs.getString("qq"));
contact.setType(rs.getString("type"));
}
return contact;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,stmt);
}
}
@Override
public boolean checkContact(String name) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = JDBCUtil.getConnection();
//进行数据库查询操作
String sql = "SELECT * FROM contact WHERE name=?";
//创建PrepareStatement
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setString(1,name);
//执行
rs = stmt.executeQuery();
if (rs.next()){
return true;
}else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
JDBCUtil.close(conn,stmt);
}
}
public ArrayList<Contact> findType(Contact c) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
//获取连接
conn = JDBCUtil.getConnection();
String sql = "SELECT * FROM contact where type=?";
//创建PreparedStatement
stmt = conn.prepareStatement(sql);
stmt.setString(1,c.getType());
//执行
rs = stmt.executeQuery();
List<Contact> list = new ArrayList<Contact>();
while(rs.next()){
c = new Contact();
c.setId(rs.getString("id"));
c.setName(rs.getString("name"));
c.setGender(rs.getString("gender"));
c.setAge(rs.getInt("age"));
c.setPhone(rs.getString("phone"));
c.setEmail(rs.getString("email"));
c.setQq(rs.getString("qq"));
c.setType(rs.getString("type"));
list.add(c);
}
return (ArrayList<Contact>) list;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}finally{
JDBCUtil.close(conn, stmt ,rs);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JSP+JavaBean+Servlet制作通讯录.zip
共63个文件
class:19个
java:19个
jsp:6个
需积分: 50 15 下载量 45 浏览量
2020-12-05
14:54:29
上传
评论 7
收藏 1.93MB ZIP 举报
温馨提示
用JSP+Servlet+JavaBean开发个人通讯录(附加数据库)实现数据的增删改查+登录/注册界面的详细;并且数据库运用了数据库连接池等详细功能;
资源详情
资源评论
资源推荐
收起资源包目录
JSP+JavaBean+Servlet制作通讯录.zip (63个子文件)
JSP+JavaBean+Servlet制作通讯录
db_addressbook.sql 3KB
address_book
.project 912B
.settings
.jsdtscope 567B
org.eclipse.wst.common.component 488B
org.eclipse.jdt.core.prefs 547B
org.eclipse.wst.common.project.facet.core.xml 344B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
cn
com
sise
utils
JDBCUtil.java 3KB
dao
ContactDao.java 655B
impl
UserDao.java 1KB
ContactDaoMySQLImpl.java 8KB
exception
NameRepeatException.java 207B
servlet
LoginServlet.java 2KB
AddContactServlet.java 2KB
ListContactServlet.java 1KB
QueryContactServlet.java 1KB
FindContactServlet.java 2KB
RegisterServlet.java 2KB
DeleteContactServlet.java 1KB
UpdateContactServlet.java 2KB
dto
User.java 465B
Contact.java 2KB
JDBC
TestJdbcServlet.java 838B
Demo1.java 2KB
service
impl
ContactServiceImpl.java 1KB
ContactService.java 411B
db.properties 109B
build
classes
cn
com
sise
utils
JDBCUtil.class 3KB
dao
ContactDao.class 618B
impl
ContactDaoMySQLImpl.class 6KB
UserDao.class 2KB
exception
NameRepeatException.class 393B
servlet
DeleteContactServlet.class 2KB
UpdateContactServlet.class 2KB
LoginServlet.class 2KB
QueryContactServlet.class 2KB
FindContactServlet.class 2KB
ListContactServlet.class 2KB
AddContactServlet.class 3KB
RegisterServlet.class 2KB
dto
User.class 974B
Contact.class 3KB
JDBC
TestJdbcServlet.class 1KB
Demo1.class 673B
service
impl
ContactServiceImpl.class 2KB
ContactService.class 497B
db.properties 109B
WebContent
login.jsp 2KB
css
background.css 102B
jsp_css.css 1KB
WEB-INF
lib
jstl-1.2.jar 405KB
dom4j-1.6.1.jar 307KB
standard.jar 287KB
mysql-connector-java-5.1.9-bin.jar 707KB
listContact.jsp 2KB
META-INF
MANIFEST.MF 39B
image
baby.jpg 415KB
register.jsp 2KB
findContact.jsp 3KB
updateContact.jsp 2KB
addContact.jsp 2KB
.classpath 911B
共 63 条
- 1
小菜鸟NNNN
- 粉丝: 72
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0