JDBC、MySQLバインド4-DELETE、UPDATE

29567 ワード

📌 JDBC、MySQLバインド4-DELETE、UPDATE

네이버 부스트코스의 웹 백엔드 수강을 기반으로 작성된 글입니다.

1.MySQLの使用


2. JDBC - DELETE


3. JDBC - UPDATE


1.MySQLの使用


RoleDao.java - deleteRole(), updateRole()


📍 RoleDao.JAvaはMySQLとRoleです.Javaクラスを使用してデータのクラスを処理します.

1.JDBCドライバをロードしてデータベースに接続する

  • Class.forNameメソッドを使用してJDBCドライバをロードし、JDBC APIを使用します.

  • 各DBのクラス名は異なるので、対応するDB名を正しく付ける必要があります.

  • データベース接続の設定
  • //드라이버 로딩
    Class.forName("com.mysql.jdbc.Driver");
    //커넥션 객체
    conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);

    2. JDBC - DELETE


    RoleDao.java - deleteRole()


    1.SQL文の作成

    //delete 된 결과 건수를 나타낼 int 형 변수 선언
    int deleteCount = 0;
    
    String sql = "delete from role where role_id = ?";			
    ps = conn.prepareStatement(sql);			
    ps.setInt(1, roleId);			
    deleteCount = ps.executeUpdate();

    2. DELETE


    🙏 roleテーブルのすべてのデータを出力しようとします.
    public class JDBCexam4 {
    
    	public static void main(String[] args) {
    
    		int roleId = 500;
    		
    		RoleDao dao = new RoleDao();	
    		
    		int deleteCount = dao.deleteRole(roleId);
    		List<Role> list = dao.getRoles();	
    		
    		System.out.println(deleteCount);		
    			
    		for(Role role : list) {
    			System.out.println(role);
    		}	
    	}
    
    }
  • role id=500のデータが削除されたかどうか
  • MySQLで
  • データが正しく削除されていることを確認

  • 3. JDBC - UPDATE


    RoleDao.java - updateRole()


    1.SQL文の作成

    //update 된 결과 건수를 나타낼 int 형 변수 선언
    int updateCount = 0;
    
    String sql = "update role set description = ? where role_id = ?";
    			
    ps = conn.prepareStatement(sql);
    			
    ps.setString(1, role.getDescription());
    ps.setInt(2, role.getRoleId());
    
    updateCount = ps.executeUpdate();

    2. UPDATE


    🙏 role=500のデータのdescription値を「CEO」に更新します.
    public class JDBCexam5 {
    
    	public static void main(String[] args) {
    		
    		int roleId = 500;
    		String description = "CEO";
    		
    		Role role = new Role(roleId, description);
    		
    		RoleDao dao = new RoleDao();
    		int updateCount = dao.updateRole(role);
    		
    		System.out.println(updateCount);
    				
    		//알맞게 데이터 수정되었는지 확인하기
    		List<Role> list = dao.getRoles();
    		
    		for(Role rolelist : list) {
    			System.out.println(rolelist);
    		}
    	}
    }
  • 更新前のロールテーブルのデータを確認
  • role=500のデータの記述値は「CEO」に更新されましたか?
  • MySQLで
  • データが正しく更新されていることを確認

  • 📕 完全なコード
    RoleDao.java - deleteRole()
    //Delete
    	public int deleteRole(Integer roleId) {
    		int deleteCount = 0;
    		
    		Connection conn = null;
    		PreparedStatement ps = null;
    		
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			
    			conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
    			
    			String sql = "delete from role where role_id = ?";
    			
    			ps = conn.prepareStatement(sql);
    			
    			ps.setInt(1, roleId);
    			
    			deleteCount = ps.executeUpdate();
    			
    		} catch (Exception e) {			
    			e.printStackTrace();
    		}finally {
    			if(ps!=null) {
    				try {
    					ps.close();
    				} catch (SQLException e) {					
    				}
    				
    			if(conn != null) {
    				try {
    					conn.close();
    				} catch (SQLException e) {
    				}
    			}
    			}
    		}
    		
    		return deleteCount;
    	}
    
    JDBCexam4.java
    public class JDBCexam4 {
    
    	public static void main(String[] args) {
    
    		int roleId = 500;
    		
    		RoleDao dao = new RoleDao();	
    		
    		int deleteCount = dao.deleteRole(roleId);
    		List<Role> list = dao.getRoles();	
    		
    		System.out.println(deleteCount);		
    			
    		for(Role role : list) {
    			System.out.println(role);
    		}	
    	}
    	
    }
    RoleDao.java - updateRole()
    //Update
    	public int updateRole(Role role) {
    		int updateCount = 0;
    		
    		Connection conn = null;
    		PreparedStatement ps = null;
    		
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    			
    			conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
    			
    			String sql = "update role set description = ? where role_id = ?";
    			
    			ps = conn.prepareStatement(sql);
    			
    			ps.setString(1, role.getDescription());
    			ps.setInt(2, role.getRoleId());			
    			
    			updateCount = ps.executeUpdate();
    			
    		} catch (Exception e) {			
    			e.printStackTrace();
    		}finally {
    			if(ps!=null) {
    				try {
    					ps.close();
    				} catch (SQLException e) {					
    				}
    				
    			if(conn != null) {
    				try {
    					conn.close();
    				} catch (SQLException e) {
    				}
    			}
    			}
    		}
    		
    		return updateCount;
    	}
    JDBCexam5.java
    public class JDBCexam5 {
    
    	public static void main(String[] args) {
    		
    		int roleId = 500;
    		String description = "CEO";
    		
    		Role role = new Role(roleId, description);
    		
    		RoleDao dao = new RoleDao();
    		int updateCount = dao.updateRole(role);
    		
    		System.out.println(updateCount);
    				
    		//알맞게 데이터 수정되었는지 확인하기
    		List<Role> list = dao.getRoles();
    		
    		for(Role rolelist : list) {
    			System.out.println(rolelist);
    		}
    	}
    	
    }