Oracle DB + Java(eclipse) (사용자에게 입력받아 sql문 입력)
개발/K-DigitalTraining 수강중2023. 3. 23. 15:49
728x90
반응형
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class UpdateJava2 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement pstmt =null;
Scanner sc = new Scanner(System.in);
try {
Class.forName("oracle.jdbc.OracleDriver");
String url= "jdbc:oracle:thin:@localhost:1521:xe";
String user = "scott";
String password = "TIGER";
con = DriverManager.getConnection(url,user,password);
// 부서번호가 55번 dname 을 WEB으로 변경
System.out.println("부서코드 입력해주세요 >>");
int deptno = sc.nextInt();
System.out.println("부서명을 입력해주세요 >>");
//next , nextLine - 둘다 String 으로 처리
//nextLine - 은 줄단위로 읽기 (enter 까지 읽는다)
//next enter - 직전까지 읽는다.
String dname = sc.next();
String sql = "update dept_temp set dname = ? where deptno = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, dname);
pstmt.setInt(2 , deptno);
int result = pstmt.executeUpdate();
if(result>0) {
System.out.println("Update successfull");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
pstmt.close();
con.close()
;
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
728x90
반응형
'개발 > K-DigitalTraining 수강중' 카테고리의 다른 글
Oracle DB ( insert , delete , update 실습) (0) | 2023.03.23 |
---|---|
Oracle DB ( DML ) (0) | 2023.03.23 |
Oracle DB ( 서브쿼리 ) (0) | 2023.03.22 |
Oracle DB ( join 실습문제 ) (0) | 2023.03.22 |
Oracle DB ( join (inner join , outer join ) (1) | 2023.03.22 |
댓글()