1. 程式人生 > 實用技巧 >Java自學-JDBC DAO

Java自學-JDBC DAO

基於JDBC設計DAO的例項

DAO=DataAccess Object

資料訪問物件

實際上就是運用了ORM中的思路,把資料庫相關的操作都封裝在這個類裡面,其他地方看不到JDBC的程式碼

步驟 1 : DAO介面

package jdbc;
  
import java.util.List;
 
import charactor.Hero;
  
public interface DAO{
    //增加
    public void add(Hero hero);
    //修改
    public void update(Hero hero);
    //刪除
    public void delete(int id);
    //獲取
    public Hero get(int id);
    //查詢
    public List<Hero> list();
    //分頁查詢
    public List<Hero> list(int start, int count);
}

步驟 2 : HeroDAO

設計類HeroDAO,實現介面DAO

  1. 把驅動的初始化放在了構造方法HeroDAO裡:

    public HeroDAO() {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }
    }

因為驅動初始化只需要執行一次,所以放在這裡更合適,其他方法裡也不需要寫了,程式碼更簡潔

  1. 提供一個getConnection方法返回連線
    所有的資料庫操作都需要事先拿到一個數據庫連線Connection,以前的做法每個方法裡都會寫一個,如果要改動密碼,那麼每個地方都需要修改。 通過這種方式,只需要修改這一個地方就可以了。 程式碼變得更容易維護,而且也更加簡潔。

     package jdbc;
     import java.sql.Connection;
      
     import java.sql.DriverManager;
     import java.sql.PreparedStatement;
     import java.sql.ResultSet;
     import java.sql.SQLException;
     import java.sql.Statement;
     import java.util.ArrayList;
     import java.util.List;
       
     import charactor.Hero;
       
     public class HeroDAO implements DAO{
       
         public HeroDAO() {
             try {
                 Class.forName("com.mysql.jdbc.Driver");
             } catch (ClassNotFoundException e) {
                 e.printStackTrace();
             }
         }
       
         public Connection getConnection() throws SQLException {
             return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8", "root",
                     "admin");
         }
       
     public int getTotal() {
         int total = 0;
         try (Connection c = getConnection(); Statement s = c.createStatement();) {
    
             String sql = "select count(*) from hero";
    
             ResultSet rs = s.executeQuery(sql);
             while (rs.next()) {
                 total = rs.getInt(1);
             }
    
             System.out.println("total:" + total);
    
         } catch (SQLException e) {
    
             e.printStackTrace();
         }
         return total;
     }
    
     public void add(Hero hero) {
    
         String sql = "insert into hero values(null,?,?,?)";
         try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
    
             ps.setString(1, hero.name);
             ps.setFloat(2, hero.hp);
             ps.setInt(3, hero.damage);
    
             ps.execute();
    
             ResultSet rs = ps.getGeneratedKeys();
             if (rs.next()) {
                 int id = rs.getInt(1);
                 hero.id = id;
             }
         } catch (SQLException e) {
    
             e.printStackTrace();
         }
     }
    
     public void update(Hero hero) {
    
         String sql = "update hero set name= ?, hp = ? , damage = ? where id = ?";
         try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
    
             ps.setString(1, hero.name);
             ps.setFloat(2, hero.hp);
             ps.setInt(3, hero.damage);
             ps.setInt(4, hero.id);
    
             ps.execute();
    
         } catch (SQLException e) {
    
             e.printStackTrace();
         }
    
     }
    
     public void delete(int id) {
    
         try (Connection c = getConnection(); Statement s = c.createStatement();) {
    
             String sql = "delete from hero where id = " + id;
    
             s.execute(sql);
    
         } catch (SQLException e) {
    
             e.printStackTrace();
         }
     }
    
     public Hero get(int id) {
         Hero hero = null;
    
         try (Connection c = getConnection(); Statement s = c.createStatement();) {
    
             String sql = "select * from hero where id = " + id;
    
             ResultSet rs = s.executeQuery(sql);
    
             if (rs.next()) {
                 hero = new Hero();
                 String name = rs.getString(2);
                 float hp = rs.getFloat("hp");
                 int damage = rs.getInt(4);
                 hero.name = name;
                 hero.hp = hp;
                 hero.damage = damage;
                 hero.id = id;
             }
    
         } catch (SQLException e) {
    
             e.printStackTrace();
         }
         return hero;
     }
    
     public List<Hero> list() {
         return list(0, Short.MAX_VALUE);
     }
    
     public List<Hero> list(int start, int count) {
         List<Hero> heros = new ArrayList<Hero>();
    
         String sql = "select * from hero order by id desc limit ?,? ";
    
         try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
    
             ps.setInt(1, start);
             ps.setInt(2, count);
    
             ResultSet rs = ps.executeQuery();
    
             while (rs.next()) {
                 Hero hero = new Hero();
                 int id = rs.getInt(1);
                 String name = rs.getString(2);
                 float hp = rs.getFloat("hp");
                 int damage = rs.getInt(4);
                 hero.id = id;
                 hero.name = name;
                 hero.hp = hp;
                 hero.damage = damage;
                 heros.add(hero);
             }
         } catch (SQLException e) {
    
             e.printStackTrace();
         }
         return heros;
     }
    }
    

更多內容,點選瞭解: 基於JDBC設計DAO的例項