본문 바로가기
🍷DataBase/🎮Oracle

[Oracle]자바 오라클 sql delete, update구문, select구문

by 김말자 2022. 12. 21.
728x90
728x90
BIG

delete구문

package helloworld;

import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;



public class m2 {
   
	
	
   static PreparedStatement pstmt;
   static Connection conn;
   private static String id;
   private static String name;
   private static double height;
   private static double weight;
   private static int age;
   private static Statement                                                                                stmt;
   private static ResultSet rs;

   public static void main(String[] args) throws SQLException{
      //1.driver loading
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@localhost:1521:xe";
      String userid = "sample";
      String pwd = "1234";
      
      try {
         Class.forName(driver);
         System.out.println("Oracle Driver Loading Success!");
         //2.connection
         conn = DriverManager.getConnection(url, userid, pwd);
         System.out.println("Oracle Connection Success!");
         
         //3.sql command
         //데이터 입력하는 부분
         Scanner sc = new Scanner(System.in);
         System.out.println("아이디를 입력하세요");
         String id = sc.next();
//         System.out.println("이름을 입력하세요");
//         String name = sc.next();
//         System.out.println("키를 입력하세요");
//         double height = sc.nextDouble();
//         System.out.println("몸무게를 입력하세요");
//         double weight = sc.nextDouble();
//         System.out.println("나이를 입력하세요");
//         int age = sc.nextInt();

         
         
         String sql = "delete from member where id=?";
         
         pstmt = conn.prepareStatement(sql);
             //    pstmt.setInt(1, age);
                 pstmt.setString(1, id);
//                 pstmt.setDouble(3, height);
//                 pstmt.setDouble(4, weight);
//                 pstmt.setInt(5, age);
                 pstmt.executeUpdate();
               
                 

          	 
                 
                 
                 
                 
                 
                 
                 
                 //4.result
         //stmt = conn.createStatement();
         
         //String quy = "select * from member";
         pstmt = conn.prepareStatement("select * from member");
         rs = pstmt.executeQuery();
         
         System.out.println("===================================");
         while(rs.next()) {
            System.out.print("아이디: " + rs.getString("id")+" ");
            System.out.print("이 름: " + rs.getString("name")+" ");
            System.out.print("키: " + rs.getDouble("height")+" ");
            System.out.print("몸무게: " + rs.getDouble("weight")+" ");
            System.out.println("나 이: " + rs.getInt("age"));
            
         }
         System.out.println("===================================");
         }catch(ClassNotFoundException e){
               e.printStackTrace();
          /*  
         }catch(NullPointerException e2) {
               e2.printStackTrace();
               */
            }catch(Exception e3) {
               e3.printStackTrace();
         }finally {
            if(rs!=null) {
               try {
                  rs.close();
               } catch (Exception e2) {
                  e2.printStackTrace();
               }
            }
            
            if (pstmt !=null) {
               try {
               pstmt.close();
               }catch (Exception e) {
                  e.printStackTrace();
               }
               if (conn !=null) {
                  try {
                  conn.close();
               }catch (Exception e) {
                     e.printStackTrace();
               }
            }
         }
      }
   }
}

update구문

package helloworld;

import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;



public class member {
   
	
	
   static PreparedStatement pstmt;
   static Connection conn;
   private static String id;
   private static String name;
   private static double height;
   private static double weight;
   private static int age;
   private static Statement                                                                                stmt;
   private static ResultSet rs;

   public static void main(String[] args) throws SQLException{
      //1.driver loading
      String driver = "oracle.jdbc.driver.OracleDriver";
      String url = "jdbc:oracle:thin:@localhost:1521:xe";
      String userid = "sample";
      String pwd = "1234";
      
      try {
         Class.forName(driver);
         System.out.println("Oracle Driver Loading Success!");
         //2.connection
         conn = DriverManager.getConnection(url, userid, pwd);
         System.out.println("Oracle Connection Success!");
         
         //3.sql command
         //데이터 입력하는 부분
         Scanner sc = new Scanner(System.in);
         System.out.println("아이디를 입력하세요");
         String id = sc.next();
//         System.out.println("이름을 입력하세요");
//         String name = sc.next();
//         System.out.println("키를 입력하세요");
//         double height = sc.nextDouble();
//         System.out.println("몸무게를 입력하세요");
//         double weight = sc.nextDouble();
         System.out.println("나이를 입력하세요");
         int age = sc.nextInt();

         
         
         String sql = "UPDATE member SET AGE = ? WHERE id = ?";
         
         pstmt = conn.prepareStatement(sql);
                 pstmt.setInt(1, age);
                 pstmt.setString(2, id);
//                 pstmt.setDouble(3, height);
//                 pstmt.setDouble(4, weight);
//                 pstmt.setInt(5, age);
                 pstmt.executeUpdate();
               
                 

          	 
                 
                 
                 
                 
                 
                 
                 
                 //4.result
         //stmt = conn.createStatement();
         
         //String quy = "select * from member";
         pstmt = conn.prepareStatement("select * from member");
         rs = pstmt.executeQuery();
         
         System.out.println("===================================");
         while(rs.next()) {
            System.out.print("아이디: " + rs.getString("id")+" ");
            System.out.print("이 름: " + rs.getString("name")+" ");
            System.out.print("키: " + rs.getDouble("height")+" ");
            System.out.print("몸무게: " + rs.getDouble("weight")+" ");
            System.out.println("나 이: " + rs.getInt("age"));
            
         }
         System.out.println("===================================");
         }catch(ClassNotFoundException e){
               e.printStackTrace();
          /*  
         }catch(NullPointerException e2) {
               e2.printStackTrace();
               */
            }catch(Exception e3) {
               e3.printStackTrace();
         }finally {
            if(rs!=null) {
               try {
                  rs.close();
               } catch (Exception e2) {
                  e2.printStackTrace();
               }
            }
            
            if (pstmt !=null) {
               try {
               pstmt.close();
               }catch (Exception e) {
                  e.printStackTrace();
               }
               if (conn !=null) {
                  try {
                  conn.close();
               }catch (Exception e) {
                     e.printStackTrace();
               }
            }
         }
      }
   }
}

잘 돌아가는 것을 알 수 있다

728x90
반응형
BIG

댓글