import java.sql.*;
public class a2_update {
//sno가 8003인놈 avr 9.99로 바꿔라
public static void main(String[] args) {
// 오라클 드라이버 변수 선언 및 사용 클래스 정의
final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
//
//접속할 데이터베이스 url 지정'
final String DB_URL = "jdbc:oracle:thin:@192.168.0.232:1521:xe";
//접속할 계정 정보
final String USER = "C##EUNSUK";
final String PASSWORD = "root";
//커넥션 변수 선언
Connection conn = null;
//쿼리 구문 변수 선언
// Statement statement = null;
//파라미터를 가지는 쿼리구문 변수 선언
PreparedStatement pstmt = null;
try{
//jdbc 드라이버 클래스 로드
Class.forName(JDBC_DRIVER);
//데이터베이스 연결
conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
//SQL 쿼리 생성
// statement = conn.createStatement();
String sql ="update student set avr = ? where sno = ?";
//update sql 쿼리문
//들어갈 파라미터 세팅은 물음표의 개수와 동일하게 설정
//결과를 담아줄 resultset변수 선언
//select -> executequery(sql);
//insert, update, delete -> executeupdate(sql)
// ResultSet resultset = statement.executeQuery(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setDouble(1,9.99);
pstmt.setString(2,"8003");
//insert delete update는 결과로 영향받은 행의 개수를 리턴한다.
int result = pstmt.executeUpdate();
//이렇게하면 result의 결과 갯수가 나오게되고
//결과출력
if(result >=1 ) System.out.println("수정되었습니다..");
else{
System.out.println("저장에 실패하였습니다.");
}
pstmt.close();
conn.close();
}catch ( SQLException se){
System.out.println(se.getMessage());
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
//정상종료되든 에러가 발생하든 객체를 제거
if(pstmt !=null){
try {
pstmt.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
}
}
댓글 영역