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平台的API调用,这些部分在Pascal中可能需要替换为相应的函数或库。 总的来说,Java2Pas是一个方便的工具,能够帮助开发者跨越Java和Pascal之间的语言障碍,提高代码复用...
"Java代码保护,防止反编译" 本文讨论了Java程序的保护方法,以防止反编译和盗版。由于Java语言的特点,使得反编译变得非常容易。因此,保护Java程序变得非常重要。本文首先讨论了保护Java程序的基本方法,然后对...
Java代码生成数字证书涉及到几个关键概念和技术,包括Java的密钥和证书管理、RSA加密算法以及非交互式证书创建。在此,我们将深入探讨这些主题,以便理解如何在Java环境中生成和使用数字证书。 1. **数字证书**:...
eclipse java 代码块折叠插件,可以用对if、for、while、function 等代码块折叠,类似vs IDE。 使用方法: (1)解压; (2)将 com.cb.eclipse.folding_1.0.6.jar 拷贝到eclipse 安装目录的插件目录(plugins目录)...
1.codeFactory: Java代码生成,依赖rapid-generator.4.0.6.jar, 支持自定义模板生成代码, 弱业务下完全解放体力劳动. 2.ssm模板实现:pojo,dao,daoimpl,service,serviceimpl,controller,各mapper.xml 代码自动生成, 3....
java代码审计-sqli.md 第一的 4年前 java代码审计-ssrf.md 第一的 4年前 java代码审计-ssti.md 第一的 4年前 java代码审计-xss.md 第一的 4年前 java代码审计-xxe.md 第一的 4年前 java代码审计-反序列化.md 添加...
公司目前使用的java代码检查表,提供出来供大家参考
一个很强大的工具, 能将c#代码片段、文件甚至工程直接转换成java代码,并能彻底解决外部引用的DLL问题,最强的是支持c#工程的直接转换,生成的Java代码质量也很不错。软件已破解,去除了未注册版最多只能转换1000行的...
java代码评审检查表:包含java常用代码审查内容
在这个"OnlyOffice官网示例Java代码"中,我们可以看到官方提供的Java代码示例,这些示例经过修改后可以直接运行,只需简单地调整服务器IP地址即可。 首先,让我们了解一下OnlyOffice的API接口。OnlyOffice 提供的...
java代码直接转化成smali代码工具java代码直接转化成smali代码工具java代码直接转化成smali代码工具java代码直接转化成smali代码工具java代码直接转化成smali代码工具java代码直接转化成smali代码工具java代码直接...
描述中的"java to c# java to csharp java2c# java代码转c#"进一步强调了转换的目标,表示用户可能已经花费了一定的时间寻找如何将Java代码转换为C#的方法,这可能包括在线搜索、阅读文档或者尝试不同的工具。...
总的来说,WinMerge作为一款优秀的Java代码比较工具,它的强大功能和易用性使其成为开发人员不可或缺的助手,尤其是在代码管理和协同开发的过程中。掌握并熟练使用WinMerge,无疑能够提升开发效率,降低出错概率,...
java代码审计常规思路和方法
Java代码的30条经验总结
当我们需要将C#代码转换为Java代码时,可能会涉及到语言特性、API映射以及代码结构的调整。下面我们将深入探讨这个过程。 标题"**C#代码转Java代码工具**"所暗示的知识点是,存在一种工具或技术能够帮助开发者将C#...
最新版java代码生成器基于springMvc+mysql 后台功能一键生成 压缩包里的jdk文件目录 请自行下载jdk1.8.0_45版本并覆盖进去 没有jdk是运行不起来的 也可以下载其他jdk版本 但是jdk1.8.0_45这个文件目录名称不要更改 ...
Java代码生成工具新版 支持Oracle、DB2、SQLServer、MYSQL、数据库。 支持Struts1、Struts2、SpringMvc、Spring、Hibernate3框架。 支持自增主键,复合主键,外键关联。 生成Hibernate POJO类,Dao类,Service类,...
java代码-使用java解决读取U盘信息的源代码 ——学习参考资料:仅用于个人学习使用!