日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發布時間:2025/3/19 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql数据库dao模式_古诗MySQL数据库DAO模式实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

整體規則

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模式实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。