mysql数据库dao模式_古诗MySQL数据库DAO模式实现

整体规则

b86f33b7a5bc99de3f617fb07f2d6faf.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值