2021/12/08 데이터베이스 업데이트

2021/12/08 데이터베이스 업데이트

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Scanner; public class ex04_update { public static void main(String[] args) { PreparedStatement psmt = null; Connection conn = null; System.out.println("업데이트"); Scanner sc = new Scanner(System.in); String id = sc.nextLine(); String pw=sc.nextLine(); String repw=sc.nextLine(); try { Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:xe"; //id와 pw를 입력받아서 info 테이블에서 해당 id를 검색하고 // pw를 입력받은 값으로 변경 String db_id = "hr"; String db_pw = "hr"; try { conn = DriverManager.getConnection(url, db_id, db_pw); String sql = "update info set pw=? where id=? and pw=?"; psmt = conn.prepareStatement(sql); psmt.setString(1, repw); psmt.setString(2, id); psmt.setString(3, pw); int count = psmt.executeUpdate(); System.out.println(count); if (count > 0) { System.out.println("업데이트 성공 "); } else { System.out.println("삭제 실패 "); } } catch (SQLException e) { e.printStackTrace(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (psmt != null) { try { psmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }

공유하기 글 요소 저작자표시

from http://onemoretry.tistory.com/37 by ccl(A) rewrite - 2021-12-08 13:02:03