ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • (JAVA / JDBC) JDBC로 JAVA와 DB 연결하기 - Oracle
    JAVA 2021. 7. 27. 16:33

    - 간단한 실습을 겸한 oracle 연동 코드

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    package day22;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    class Student{
        int snum;
        String sname;
        
        Student(int snum, String sname){
            this.snum = snum;
            this.sname = sname;
        }
     
        @Override
        public String toString() {
            return "학생정보 [번호=" + snum + ", 이름=" + sname + "]";
        }
            
    }
    public class Test2 {
     
       public static void main(String[] args) {
          String DName="oracle.jdbc.driver.OracleDriver";
     
          String url="jdbc:oracle:thin:@localhost:1521:xe";
          String user="oh";
          String password="0000";
     
          Connection conn=null;
          Statement stmt=null;
          
          Scanner sc = new Scanner(System.in);
          
          ArrayList<Student> data = new ArrayList();
     
          try {
             Class.forName(DName);
     
             conn=DriverManager.getConnection(url, user, password);
     
             stmt=conn.createStatement();
             
             // 1번 출력
             ResultSet rs=stmt.executeQuery("select * from student");
             while(rs.next()) {
                int snum = rs.getInt("snum");
                String sname = rs.getString("sname");
                data.add(new Student(snum, sname));
             }
             
             for(Student v : data) {
                 System.out.println(v);
             }
             rs.close();
             
             // 2번 수정
             System.out.println("===== 이름 수정 =====");
             
             for(Student v : data) {
                 System.out.println(v.sname + "의 이름을 무엇으로 수정하시겠습니까?");
                 System.out.print("입력: ");
                 String sname = sc.next();
                 String sql = "UPDATE student SET sname = '" + sname +"' WHERE snum = " + v.snum;
                 stmt.executeUpdate(sql);
             }
             
             System.out.println("이름에 김이 들어간 녀석을 찾아볼까?");
             rs=stmt.executeQuery("select sname from student where sname LIKE '%김%'");
             
             while(rs.next()) {
                 System.out.print(rs.getString("sname"+ " ");
             }
             rs.close();
             
             
          }catch(Exception e) {
             System.out.println("예외발생!");
          }finally {
             try {
                stmt.close();
                conn.close();
             } catch (SQLException e) {
                e.printStackTrace();
             }
          }
       }
     
    }
    cs
Designed by Tistory.