관리 메뉴

moozi

orcle jdbc 기초2 본문

TIS_2017/응용sw_1기

orcle jdbc 기초2

moozi 2016. 12. 26. 11:31

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class JDBC01 {


public static void main(String[] args) {

Connection conn = null; // 연결객체

        ResultSet rs = null;    // select한 결과를 저장하는 객체

        Statement stmt = null;  // sql을 실행하기 위한 객체 

        String url = null;      // 서버 url

        String id = "hr";       // ID

        String pw = "1234";     // PW

        

        url ="jdbc:oracle:thin:@localhost:1521:orcl";

        try {

Class.forName("oracle.jdbc.driver.OracleDriver");// jdbc driver load

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

        try {

conn = DriverManager.getConnection(url,id,pw);// 연결

} catch (SQLException e) {

e.printStackTrace();

}

        

        System.out.println("연결되었습니다.");

        

        try {

stmt = conn.createStatement(); // stament객체 생성

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        try {

rs = stmt.executeQuery("select * from employees where employee_id=100");  //쿼리실행

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


        try {

while(rs.next()){ // 한행씩 레코드를 읽어옴. Fetch. 데이터가 있으면 true.

   String employeeId = rs.getString(1); // 컬럼index로 데이터를 읽어옴.1번부터

   String firstName = rs.getString(2);

   String lastName = rs.getString(3);

   System.out.println(employeeId+", " + firstName +", "+lastName);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


        try {

stmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

        try {

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


}



'TIS_2017 > 응용sw_1기' 카테고리의 다른 글

java연습문제06풀이  (0) 2016.12.27
jdbc연습문제01  (0) 2016.12.26
oracle jdbc 기초  (0) 2016.12.26
oracle연습문제05  (0) 2016.12.23
오라클 DB생성  (0) 2016.12.23
Comments