一個DAO的實現

  • 接口
package jdbctest;

import java.util.List;

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(int start, int count);
	 
}
  • 接口實現
package jdbctest;

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 java.sql.Connection;

public class HeroDAO implements DAO{
	public HeroDAO(){
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");//mysql8中註冊驅動與mysql5不一樣
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//getConnections()函數返回conn對象
	public Connection getConnections() throws SQLException{
		//註冊驅動,獲得連接Connection
		return DriverManager.getConnection("jdbc:mysql://192.168.88.120:3306:3306/db_hero?characterEncoding=UTF-8","root","123456");
	}
	public int getTotal(){
		int total = 0;
		//創建連接對象和連接語句對象
		try(Connection c = getConnections();Statement s = c.createStatement();){
			String sql = "select count(*) from hero";
			//executeQuery()方法僅僅用於select語句,返回對象。execute()用於執行所有語句,返回boolean類型。executeUpdate()用於執行delete,update,insert語句,返回int
			ResultSet rs = s.executeQuery(sql);
			while(rs.next()){
				//next()方法使ResultSet的遊標向下移動,獲取int列的數據,參數1是代表第一列
				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 = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
        	//讓指定位置(0,1,2,3)的參數設定String,float,int類型
            ps.setString(1, hero.name);
            ps.setFloat(2, hero.hp);
            ps.setInt(3, hero.damage);
  
            ps.execute();
            //需要獲取插入記錄的自增主鍵,這時候通常用getGeneratedKeys()方法獲取主鍵
            ResultSet rs = ps.getGeneratedKeys();
            if (rs.next()) {
            	//getInt(column index)獲取當前行指定列上的值,參數就是列數,列數從1開始,而不是0
                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 = getConnections(); 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){
		String sql = "delete from hero where id = ?";
		try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
			ps.setInt(1,id);
			ps.execute();

	}catch(SQLException e){
		e.printStackTrace();
		}
	}

	@Override
	public List<Hero> list(int start, int count) {
		// TODO Auto-generated method stub
		ArrayList<Hero> heros = new ArrayList();
		String sql = "select *from hero limit ?,?";
		 try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
			 ps.setInt(1,start);
			 ps.setInt(2,count);
			 ResultSet rs = ps.executeQuery();
			 while(rs.next()){
				 Hero hero = new Hero();
				 hero.id = rs.getInt(1);
				 hero.name = rs.getString(2);
				 hero.hp = rs.getFloat(3);
				 hero.damage = rs.getInt(4);
				 heros.add(hero);
			 }
		 } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return heros;
	}
	@Override
	public Hero get(int id) {
		// TODO Auto-generated method stub
		Hero hero = null;
		String sql = "select *from hero where id = ?";
		try (Connection c = getConnections(); PreparedStatement ps = c.prepareStatement(sql);) {
			//先設置值
			ps.setInt(1,id);
			//再進行結果集的操作
			ResultSet rs =  ps.executeQuery();
			if(rs.next()){
				hero = new Hero();
				hero.id = rs.getInt(1);
				hero.name = rs.getString(2);
				hero.hp = rs.getFloat(3);
				hero.damage = rs.getInt(4);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hero;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章