자바 -> Oracle DB 연결 코드
개발/K-DigitalTraining 수강중2023. 3. 21. 17:20
728x90
반응형
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Connect {
public static void main(String[] args) {
Connection con= null;
PreparedStatement pstmt = null; // sql 전달
ResultSet rs = null; // select 결과 저장
try {
Class.forName("oracle.jdbc.OracleDriver");
// "jdbc:oracle:thin:@ 고정
// localhost:1521:xe
// localhost 127.0.0.1 (내 컴퓨터)
// 1521 기본 포트번호
// xe 기본 sid 이름
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "scott";
String password = "TIGER";
con = DriverManager.getConnection(url, user, password);
// if (con!=null) {
// System.out.println("연결되었습니다");
// }
//emp 테이블에 있는 내용 가져오기
String sql = "select * from emp";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString("ename")+"\t");
System.out.print(rs.getString(3)+"\t");
System.out.print(rs.getInt("mgr")+"\t");
System.out.print(rs.getString(5)+"\t");
System.out.print(rs.getInt(6)+"\t");
System.out.print(rs.getInt(7)+"\t");
System.out.println(rs.getInt("deptno")+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
728x90
반응형
'개발 > K-DigitalTraining 수강중' 카테고리의 다른 글
Oracle DB ( join 실습문제 ) (0) | 2023.03.22 |
---|---|
Oracle DB ( join (inner join , outer join ) (1) | 2023.03.22 |
Oracle DB 실습문제 (0) | 2023.03.21 |
Oracle DB( 오라클함수 - Null처리함수 , decode함수 , case문 ) (0) | 2023.03.21 |
Oracle DB ( 오라클 함수- 형변환 함수 ) (1) | 2023.03.21 |
댓글()