package com.czz.daosimpl;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* derbyDAO
*
* @author Marshal Wu
*
*/
public class DerbyUserDao implements ManagerUserDao {
private Connection getConnection() throws SQLException {
Connection connection = DriverManager
.getConnection("jdbc:derby://localhost/test;user=test;password=test");
connection.setAutoCommit(false);
return connection;
}
public void delete(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("delete from user_info where id=?");
statement.setLong(1, user.getId());
statement.execute();
statement.close();
connection.commit();
} catch (SQLException e) {
this.rollback(connection);
throw new RegisterException(e);
} finally {
this.close(connection);
}
}
public User get(long id) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("select id,user_name,email,birthday from user_info where id=?");
statement.setLong(1, id);
ResultSet resultSet = statement.executeQuery();
User user = null;
if (resultSet.next()) {
user = getUser(resultSet);
}
statement.close();
connection.commit();
return user;
} catch (SQLException e) {
this.rollback(connection);
throw new RegisterException(e);
} finally {
this.close(connection);
}
}
private User getUser(ResultSet resultSet) throws SQLException {
User user;
user = new User();
user.setId(resultSet.getLong("id"));
user.setName(resultSet.getString("user_name"));
user.setEmail(resultSet.getString("email"));
user.setBirthday(resultSet.getDate("birthday"));
return user;
}
public List<User> getAll() {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("select id,user_name,email,birthday from user_info");
ResultSet resultSet = statement.executeQuery();
List<User> list = new ArrayList<User>();
while (resultSet.next()) {
User user = getUser(resultSet);
list.add(user);
}
statement.close();
connection.commit();
return list;
} catch (SQLException e) {
this.rollback(connection);
throw new RegisterException(e);
} finally {
this.close(connection);
}
}
public void save(User user) {
if (user.getId() == 0) {
this.create(user);
} else {
this.update(user);
}
}
private void update(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("update user_info set user_name=?,email=?,birthday=? where id=?");
int index = 1;
statement.setString(index++, user.getName());
statement.setString(index++, user.getEmail());
statement.setDate(index++, new Date(user.getBirthday().getTime()));
statement.setLong(index++, user.getId());
statement.execute();
statement.close();
connection.commit();
} catch (SQLException e) {
this.rollback(connection);
throw new RegisterException(e);
} finally {
this.close(connection);
}
}
private void create(User user) {
Connection connection = null;
try {
connection = this.getConnection();
PreparedStatement statement = connection
.prepareStatement("insert into user_info(user_name,email,birthday) values(?,?,?)");
int index = 1;
statement.setString(index++, user.getName());
statement.setString(index++, user.getEmail());
if (user.getBirthday() == null) {
statement.setDate(index++, null);
} else {
statement.setDate(index++, new Date(user.getBirthday()
.getTime()));
}
statement.execute();
statement.close();
user.setId(this.getId(connection));
connection.commit();
} catch (SQLException e) {
this.rollback(connection);
throw new RegisterException(e);
} finally {
this.close(connection);
}
}
private int getId(Connection connection) throws SQLException {
CallableStatement callableStatement = connection
.prepareCall("values identity_val_local()");
ResultSet resultSet = callableStatement.executeQuery();
resultSet.next();
int id = resultSet.getInt(1);
resultSet.close();
callableStatement.close();
return id;
}
private void rollback(Connection connection) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException e) {
}
}
}
private void close(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
}
}
}
public void deleteAll() {
}
}
分享到:
相关推荐
JAVA代码检查工具(开源) JAVA代码检查工具(开源) JAVA代码检查工具(开源) JAVA代码检查工具(开源) JAVA代码检查工具(开源)
将java代码生成html并且高亮显示的类将java代码生成html并且高亮显示的类将java代码生成html并且高亮显示的类
日历java代码日历java代码日历java代码
JAVA代码反编译工具,把class反编译成.java文件
JAVA代码优化工具,
java代码学校代码 java代码学校代码 java代码学校代码
朴素贝叶斯java代码参考朴素贝叶斯java代码参考
protobuf java代码生成,protobuf java代码生成,protobuf java代码生成,protobuf java代码生成
java代码评审检查表:包含java常用代码审查内容
wsdl生成java代码的工具,运行wsdl2java.cmd ,可根据代码的实际需求修改成路径, wsdl生成java代码的工具,运行wsdl2java.cmd ,可根据代码的实际需求修改成路径
powerDesigner 导出 JAVA 代码
java代码自动生成工具,可按个人需求自由扩展。
Java代码的30条经验总结
贪吃蛇网络版 java代码
最新版java代码生成器基于springMvc+mysql 后台功能一键生成 压缩包里的jdk文件目录 请自行下载jdk1.8.0_45版本并覆盖进去 没有jdk是运行不起来的 也可以下载其他jdk版本 但是jdk1.8.0_45这个文件目录名称不要更改 ...
实现java代码生成cer证书文件,然后可以通过代码获取 不通过keytool工具。生成密钥对。使用公钥加密。
java代码-使用java解决读取U盘信息的源代码 ——学习参考资料:仅用于个人学习使用!
Java代码生成工具新版 支持Oracle、DB2、SQLServer、MYSQL、数据库。 支持Struts1、Struts2、SpringMvc、Spring、Hibernate3框架。 支持自增主键,复合主键,外键关联。 生成Hibernate POJO类,Dao类,Service类,...
阿里巴巴Java代码规范
java2python--java代码转python工具