package net.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import net.entity.Contact;
public class AddressBookService {
private static String driver = "org.apache.derby.jdbc.ClientDriver";
private static String url = "jdbc:derby://localhost:1527/address;create=true";
private static String user = "robin";
private static String password = "robin";
/**
* 数据库连接管理方法
*
* @return 数据库连接
* @throws StoreException
* 数据库连接失败
*/
public static Connection getConnection() throws StoreException {
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new StoreException("驱动没有安装!", e);
} catch (SQLException e) {
e.printStackTrace();
throw new StoreException("连接失败!", e);
}
}
public int add(Contact contact) throws ServiceException {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
System.out.println(conn.getClass().getName());
String idSql = "select max(id)+1 as nextId from contact";
Statement st = conn.createStatement();
System.out.println(st.getClass().getName());
ResultSet rs = st.executeQuery(idSql);
int id = 1;
while (rs.next()) {
id = rs.getInt("nextId");
// id = rs.getInt(1);
}
contact.setId(id);
rs.close();
st.close();
String sql = "insert into contact "
+ "(id, name, address, phone, qq) " + "values (?,?,?,?,?)";
// String sql = "insert into contact (id, name, address, phone, qq)
// values (?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, contact.getId());
ps.setString(2, contact.getName());
ps.setString(3, contact.getAddress());
ps.setString(4, contact.getPhone());
ps.setString(5, contact.getQq());
ps.executeUpdate();
conn.commit();
return contact.getId();
} catch (Exception e) {
if (conn != null)
try {
conn.rollback();
} catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
throw new ServiceException(e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void remove(Contact contact) throws ServiceException {
remove(contact.getId());
}
public void remove(Integer id) throws ServiceException {
// delete from contact where id=?
Connection conn = null;
try {
conn = getConnection();
String sql = "delete from contact where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id.intValue());
ps.executeUpdate();
} catch (StoreException e) {
e.printStackTrace();
throw new ServiceException(e);
} catch (SQLException e) {
e.printStackTrace();
throw new ServiceException(e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void update(Contact contact) throws ServiceException {
// update contact ...
Connection conn = null;
try {
conn = getConnection();
String sql = "update contact set name=?, address=?, phone=?, qq=? "
+ "where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, contact.getName());
ps.setString(2, contact.getAddress());
ps.setString(3, contact.getPhone());
ps.setString(4, contact.getQq());
ps.setInt(5, contact.getId());
ps.executeUpdate();
} catch (StoreException e) {
e.printStackTrace();
throw new ServiceException(e);
} catch (SQLException e) {
e.printStackTrace();
throw new ServiceException(e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public Contact findById(Integer id) throws ServiceException {
Connection conn = null;
try {
conn = getConnection();
Contact contact = null;
String sql = "select id, name, address, phone, qq from contact "
+ "where id=? ";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
contact = new Contact();
contact.setId(new Integer(rs.getInt("id")));
contact.setName(rs.getString("name"));
contact.setAddress(rs.getString("address"));
contact.setPhone(rs.getString("phone"));
contact.setQq(rs.getString("qq"));
}
return contact;
} catch (StoreException e) {
e.printStackTrace();
throw new ServiceException(e);
} catch (SQLException e) {
e.printStackTrace();
throw new ServiceException(e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List findByName(String name) throws ServiceException {
Connection conn = null;
try {
conn = getConnection();
List list = new ArrayList();// LinkedList() Vector()
String sql = "select id, name, address, phone, qq from contact "
+ "where name like ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "%" + name + "%");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Contact contact = new Contact();
contact.setId(new Integer(rs.getInt("id")));
contact.setName(rs.getString("name"));
contact.setAddress(rs.getString("address"));
contact.setPhone(rs.getString("phone"));
contact.setQq(rs.getString("qq"));
list.add(contact);
}
return list;
} catch (StoreException e) {
e.printStackTrace();
throw new ServiceException(e);
} catch (SQLException e) {
e.printStackTrace();
throw new ServiceException(e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Contact> findAll() throws ServiceException {
Connection conn = null;
try {
conn = getConnection();
List<Contact> list = new ArrayList<Contact>();// LinkedList()
// Vector()
String sql = "select id, name, address, phone, qq from contact ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Contact contact = new Contact();
contact.setId(new Integer(rs.getInt("id")));
contact.setName(rs.getString("name"));
contact.setAddress(rs.getString("address"));
contact.setPhone(rs.getString("phone"));
contact.setQq(rs.getString("qq"));
list.add(contact);
}
return list;
} catch (StoreException e) {
e.printStackTrace();
throw new ServiceException(e);
} catch (SQLException e) {
e.printStackTrace();
throw new ServiceException(e);
} finally {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java通信簿
共94个文件
dat:66个
class:10个
java:9个
需积分: 4 24 下载量 101 浏览量
2008-08-20
15:52:08
上传
评论
收藏 125KB RAR 举报
温馨提示
初学者自己写的小东西 只是为了赚积分
资源详情
资源评论
资源推荐
收起资源包目录
Address3.rar (94个子文件)
Address3
.project 556B
derby.log 464B
derby.sql 145B
address
log
log.ctrl 48B
logmirror.ctrl 48B
log1.dat 1024KB
db.lck 38B
seg0
c51.dat 8KB
c71.dat 8KB
c3e1.dat 8KB
c3b1.dat 8KB
c10.dat 8KB
c331.dat 8KB
c380.dat 8KB
c361.dat 8KB
c2d0.dat 8KB
c3f1.dat 8KB
ca1.dat 24KB
c111.dat 8KB
c60.dat 8KB
c411.dat 8KB
c391.dat 8KB
c2f0.dat 8KB
c1f1.dat 8KB
c1a1.dat 8KB
c400.dat 8KB
c31.dat 8KB
c340.dat 8KB
c281.dat 8KB
c3c0.dat 8KB
c221.dat 8KB
c141.dat 8KB
c2c1.dat 8KB
cc0.dat 8KB
c311.dat 8KB
c241.dat 8KB
c1e0.dat 8KB
c371.dat 8KB
c41.dat 16KB
c200.dat 8KB
c130.dat 8KB
cf0.dat 8KB
c101.dat 8KB
c2e1.dat 8KB
c81.dat 8KB
c251.dat 16KB
cb1.dat 8KB
c3d1.dat 8KB
c321.dat 8KB
cd1.dat 8KB
c290.dat 8KB
c271.dat 8KB
c211.dat 8KB
c150.dat 12KB
c2b1.dat 8KB
c260.dat 8KB
c3a1.dat 8KB
c191.dat 16KB
c1c0.dat 8KB
c180.dat 32KB
ce1.dat 8KB
c20.dat 24KB
c90.dat 20KB
c161.dat 8KB
c121.dat 8KB
c351.dat 8KB
c1b1.dat 16KB
c1d1.dat 8KB
c171.dat 8KB
c300.dat 8KB
c2a1.dat 8KB
c230.dat 336KB
tmp
service.properties 880B
bin
net
ui
MainApp.class 4KB
MainApp$1.class 850B
service
AddressBookService.class 7KB
ServiceException.class 732B
StoreException.class 726B
ServiceTest.class 1KB
entity
Contact.class 2KB
ContactTest.class 1KB
StringTest.class 2KB
JavaTest.class 913B
src
net
ui
MainApp.java 4KB
service
ServiceTest.java 815B
StoreException.java 508B
AddressBookService.java 7KB
ServiceException.java 518B
entity
Contact.java 2KB
ContactTest.java 611B
JavaTest.java 486B
StringTest.java 1KB
.classpath 286B
db.sql 141B
共 94 条
- 1
e_spark
- 粉丝: 0
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0