整体规则

step1

DBHelper工具类,一般不用实例化,因此可以采用Singleton或者是将构造方法私有化。

/**

* Created by chuiyuan on 2/17/16.

* 工具类,一般不要实例化,此时可以采用单例设计模式,或者将构造方法私有化

*/

public class DBHelper {

public static String url ;

public static String username ;

public static String password ;

public static String driver ;

//prrty file

private static ResourceBundle rb =

ResourceBundle.getBundle("db-config");

//private Connection conn = null ;

private DBHelper(){

}

/**

* 为避免重复代码,使用静态代码块:只会在类加载的时候执行一次。

*/

static {

try{

url = rb.getString("jdbc.url");

username = rb.getString("jdbc.username");

password = rb.getString("jdbc.password");

driver = rb.getString("jdbc.driver");

Class.forName(driver);

}catch (Exception e ){

e.printStackTrace();

}

}

//get a connection with mysql

public static Connection getConnection(){

Connection conn = null ;

try {

conn = DriverManager.getConnection(url,username,password);

}catch (SQLException e ){

e.printStackTrace();

}

return conn ;

}

/**

* close

* @param rs

* @param stmt

* @param conn

*/

public static void close(ResultSet rs , Statement stmt ,Connection conn){

try {

if (rs!= null) rs.close();

if (stmt!= null) stmt.close();

if (conn!= null) conn.close();

}catch (SQLException e ){

e.printStackTrace();

}

}

}

Step2

DAO接口。

/**

* Created by chuiyuan on 2/17/16.

* interface for CRUD of Poem

*/

public interface PoemDao {

public void add (Poem poem) throws SQLException;

public void update (Poem poem) throws SQLException;

public void delete(int id) throws SQLException;

public Poem findById(int id) throws SQLException ;

public List findAll() throws SQLException ;

}

Step3

PoemDaoImpl实现step2中的接口。

/**

* Created by chuiyuan on 2/17/16.

*/

public class PoemDaoImpl implements PoemDao {

public void add(Poem poem) throws SQLException {

Connection conn = null ;

PreparedStatement ps = null ;

String sql = "insert into poemtable" +

"(dynasty, category, title, author, content, href, translation)"+

" values (?, ?, ?, ?, ?, ?, ?)";

try {

conn = DBHelper.getConnection() ;

ps = conn.prepareStatement(sql);

ps.setString(1,poem.getDynasty());

ps.setString(2,poem.getCategory());

ps.setString(3,poem.getTitle());

ps.setString(4,poem.getAuthor());

ps.setString(5,poem.getContent());

ps.setString(6,poem.getHref());

ps.setString(7,poem.getTranslation());

ps.executeUpdate();

}catch (SQLException e){

e.printStackTrace();

throw new SQLException("add poem failed");

}finally {

DBHelper.close(null, ps,conn);

}

}

public void update(Poem poem) throws SQLException {

Connection conn = null;

PreparedStatement ps = null;

String sql = "update poemtable set dynasty=?, category=?, title=?," +

" author=?, content=?, href=? ,translation=? where id=?";

try {

conn = DBHelper.getConnection();

ps = conn.prepareStatement(sql);

ps.setString(1,poem.getDynasty());

ps.setString(2,poem.getCategory());

ps.setString(3,poem.getTitle());

ps.setString(4,poem.getAuthor());

ps.setString(5,poem.getContent());

ps.setString(6,poem.getHref());

ps.setString(7,poem.getTranslation());

ps.executeUpdate();

}catch (SQLException e){

e.printStackTrace();

throw new SQLException("update poem failed");

}finally {

DBHelper.close(null,ps,conn);

}

}

public void delete(int id) throws SQLException {

Connection conn = null;

PreparedStatement ps = null;

String sql = "delete from poemtable where id=?";

try {

conn = DBHelper.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1,id);

ps.executeUpdate();

}catch (SQLException e){

e.printStackTrace();

throw new SQLException("delete poem failed");

}finally {

DBHelper.close(null,ps, conn);

}

}

public Poem findById(int id) throws SQLException {

Connection conn = null ;

PreparedStatement ps = null;

ResultSet rs = null;

Poem poem = null;

String sql = "select dynasty,catetogry,title,author,content," +

"href from poemtable where id=?";

try {

conn = DBHelper.getConnection();

ps = conn.prepareStatement(sql);

ps.setInt(1, id);

rs = ps.executeQuery();

if (rs.next()){

poem = new Poem() ;

poem.setDynasty(rs.getString(1));

poem.setCategory(rs.getString(2));

poem.setTitle(rs.getString(3));

poem.setAuthor(rs.getString(4));

poem.setContent(rs.getString(5));

poem.setHref(rs.getString(6));

}

}catch (SQLException e){

e.printStackTrace();

throw new SQLException("find by id failed");

}finally {

DBHelper.close(rs,ps, conn);

}

return poem;

}

public List findAll() throws SQLException {

Connection conn = null ;

PreparedStatement ps = null ;

ResultSet rs = null ;

Poem poem = null ;

List poemList = new ArrayList();

String sql = "select dynasty,catetogry,tie,author,content," +

"href from poemtable";

try {

conn = DBHelper.getConnection();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

while (rs.next()){

poem = new Poem() ;

poem.setDynasty(rs.getString(1));

poem.setCategory(rs.getString(2));

poem.setTitle(rs.getString(3));

poem.setAuthor(rs.getString(4));

poem.setContent(rs.getString(5));

poem.setHref(rs.getString(6));

poemList.add(poem);

}

}catch (SQLException e){

e.printStackTrace();

throw new SQLException("findAll failed");

}finally {

DBHelper.close(rs, ps, conn);

}

return poemList ;

}

}

step4

AppMain中的调用。

//store to mysql

PoemDao poemDao = new PoemDaoImpl() ;

for (Poem poem: poemList){

try {

poemDao.add(poem);

}catch (SQLException e){

e.printStackTrace();

}

}

原文:http://www.cnblogs.com/chuiyuan/p/5200498.html

mysql数据库dao模式_古诗MySQL数据库DAO模式实现相关推荐

  1. 数据库创建函数_达梦数据库创建UUID函数

    数据库创建函数_达梦数据库创建UUID函数 接触达梦数据库有一段时间了,整理了一些资料,今天分享一下达梦数据UUID自定义函数 UUID函数定义 很多数据库都有提供UUID函数,可是接触达梦数据库后, ...

  2. 易语言mysql怎么写字段值_易语言数据库怎么加字段 数据库添加字段说明

    易语言将access数据库作为数据源怎么添加记录到access? .版本 2 .支持库 eDB 数据库连接1.连接Access (取运行目录 () + "数据库名称", " ...

  3. mysql数据库范围之内_是mysql范围

    MySQL数据类型-decimal详解 1.首先,对于精度比较高的东西,比如money,我会用decimal类型,不会考虑float,double,因为他们容易产生误差,numeric和decimal ...

  4. mysql 最大导入限制_分享mysql导入.sql 数据库文件最大限制的修改

    MySQL导进.sql文件的限制题目 本人电脑上拆的PHPNow在导进数据库斗劲年夜(年夜于32M)时碰着毛病,不能导进. 找遍天理要发: php.ini配置文件中有三处天方需要改动: upload_ ...

  5. mysql executequery返回值_使用executequery数据库

    iOS数据库操作之FMDB SQLite一种轻量级关系数据库,在嵌入式系统中使用比较广泛. 在iOS中使用SQLite需要添加库libsqlite3.0.dylib,并引入头文件#import FMD ...

  6. 【MySQL系列】数据库基础学习_简单认识数据库

    「前言」文章内容大致是数据库基础,以及数据库的基本知识. 「归属专栏」MySQL 「主页链接」个人主页 「笔者」枫叶先生(fy) 「枫叶先生有点文青病」「句子分享」 我见青山多妩媚,料青山.见我应如是 ...

  7. mysql数据库建仓范式_存mysql个数

    MySQL学习笔记之数据类型详解 注:以下内容针对MySQL5.0及以上版本 MySQL的数据类型非常多,选择正确的数据类型对于获得高性能至关重要,本文是我结合网上看到的一些blog加上<高性能 ...

  8. mysql 导入设置编码_从MySQL导出导入数据库的命令实例及设置会话字符编码

    1.导入导出 1.mysql导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc > ...

  9. mysql数据库连接配置路径_[zz]MySQL数据库主从同步安装与配置总结

    注意:本文出自"阿飞"的博客 ,如果要转载本文章,请与作者联系! 并注明来源: http://blog.sina.com.cn/s/blog_49fd52cf0100pog2.ht ...

最新文章

  1. JAVA之间的引用传递
  2. POJ3345 Bribing FIPA 【背包类树形dp】
  3. ToStringBuilder学习(三):readResolve()方法与序列化
  4. 动态数组vector
  5. 使用.NET开发的数据库小工具 DbTool
  6. 基于 Spring Cloud 完整的微服务架构实战
  7. leetcode 1319. 连通网络的操作次数(并查集)
  8. --allow-file-access-from-files 命令的使用
  9. php基础教程 第五章,php基础教程——5数据库总结_PHP教程
  10. Mybatis原理解析(二)SqlSession的创建过程
  11. Windows系统优化软件 | 这10款功能超级强大!界面优美!值得收藏
  12. 天气预报接口_JMeter 接口自动化测试篇 26
  13. java丶对数组值按首字母进行排序
  14. pdf java解析_用java如何解析pdf文件
  15. 计算机网络CiscoPacket Tracer实验
  16. Open3DCGAL DSM(数字表面模型)
  17. Python多路IO复用之select
  18. php云片网api的运用,如何实现php调用云片网接口发送短信
  19. 笔记1:VC获取系统时间的方法
  20. 2014 ChinaJoy落下帷幕 十大年度热门事件盘点

热门文章

  1. Canal Adapter二次开发,实现MySQL实时同步到Redis
  2. df 命令查看linux磁盘空间
  3. 对象属性结构赋值_(六)面向对象-下
  4. java多线程中volatile关键字
  5. 32岁学python 人工智能_python深入学习好还是直接学人工智能好?
  6. 天气预报c是什么意思_大雪节气将至,为什么老话说:寒风迎大雪,三九天气暖?...
  7. php怎么添加会员卡,怎么在微信公众号中添加一个会员卡领取功能
  8. dmp导入数据 oracle_DM数据库的安装使用
  9. c语言fsetpos是什么,fsetpos - [ C语言中文开发手册 ] - 在线原生手册 - php中文网
  10. qt 4.8.4 linux,Tslib和Qt 4.8.4与在开发板上的移植