package com.addressbook.model;
import java.sql.SQLException;
import java.util.List;
public class AddressBO {
public List findByProperty(){
java.sql.Connection con=null;
java.sql.PreparedStatement pstmt=null;
java.sql.ResultSet rs=null;
try {
con=com.addressbook.db.DBConnection.getConnection();
String sql="select * from addressbook where";
if(this.name!=null || !this.name.equals("")){
sql+=" name like '%"+this.name+"%' and";
}
if(this.phone!=null || !this.phone.equals("")){
sql+=" phone like '%"+this.phone+"%' and";
}
if(this.address!=null || !this.address.equals("")){
sql+=" address like '%"+this.address+"%' and";
}
pstmt=con.prepareStatement(sql.substring(0,sql.length()-3) +" order by id");
rs=pstmt.executeQuery();
java.util.List list=new java.util.ArrayList();
while(rs.next()){
AddressBO ab=new AddressBO();
ab.setAddress(rs.getString("address"));
ab.setId(rs.getInt("id"));
ab.setName(rs.getString("name"));
ab.setPhone(rs.getString("phone"));
list.add(ab);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public void insert(){
java.sql.Connection con=null;
java.sql.PreparedStatement pstmt=null;
try {
con=com.addressbook.db.DBConnection.getConnection();
pstmt=con.prepareStatement("insert into addressbook values(?,?,?)");
pstmt.setString(1, this.name);
pstmt.setString(2, this.phone);
pstmt.setString(3,this.address);
pstmt.executeUpdate();
//rs=pstmt.executeQuery();
//return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//return null;
}
public List findAll(){
java.sql.Connection con=null;
java.sql.PreparedStatement pstmt=null;
java.sql.ResultSet rs=null;
try {
con=com.addressbook.db.DBConnection.getConnection();
pstmt=con.prepareStatement("select * from addressbook");
rs=pstmt.executeQuery();
java.util.List list=new java.util.ArrayList();
while(rs.next()){
AddressBO ab=new AddressBO();
ab.setAddress(rs.getString("address"));
ab.setId(rs.getInt("id"));
ab.setName(rs.getString("name"));
ab.setPhone(rs.getString("phone"));
list.add(ab);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
rs.close();
pstmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
private String name;
private String phone;
private String address;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
addressbook(struts增删改查例子)
需积分: 9 18 浏览量
2009-02-07
10:55:47
上传
评论
收藏 299KB RAR 举报
gg_lrj
- 粉丝: 1
- 资源: 5
最新资源
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- 2023-04-06-项目笔记 - 第一百十五阶段 - 4.4.2.113全局变量的作用域-113 -2024.04.26
- htmlzwbjq_downyi.com.zip
- 无头单向非循环链表的实现(Test.c)
- 无头单向非循环链表的实现(SList.c)
- 浏览器重定向插件更新文件
- SSA-BP麻雀算法优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
- 粒子群算法优化BP神经网络PSO-BP的MATLAB代码(数值预测)
- 基于Springboot的一起看书平台.zip
- 无头单向非循环链表的实现(SList.h)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈