Jam's story
[JDBC] days01 본문
jdbc란
데이터베이스에 연결 및 작업을 하기 위한 자바 표준 인터페이스
db에 연결하는 순서
1.Class forName() 드라이브 로딩
2.driver manager 클래스의 get Connection 매서드를 사용해서 connection 객체를 얻어온다
3)필요한 작업 -crud
4)연결종료
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 오류 발생
[자바프로젝트]-[preferences]-[java build path]->add External Jars ->ojdbc6.jar 추가
ojdbc6.jar 위치 : C:\app\oracle\product\11.2.0\server\jdbc\lib
강사님은 sqlexception no sutiable driver found for jdbc.oracle.thin:@localhost:1521:xe 오류발생
-> :을 ,로 적으셨다.
Connection conn을 사용해서 생성한다.
ㄱ. Statement 기본객체
ㄴ.PreparedStatement 바인딩 변수를 사용해서 쿼리 실행 객체
ㄷ.CallabelStatement 저장 프로시저를 호출해서 쿼리 실행
읽어올 데이터가 있을 때까지 읽어온다 .
while(rs.next()) {
//다음(next)레코드를 읽어와서 출력
int deptno=rs.getInt(1);
String dname=rs.getString("dname");
String loc=rs.getString("loc");
//rs.getInt(1); //칼럼인덱스의 첫번째를 출력
System.out.printf("%d\n%s\n%s\n", deptno,dname,loc);
}//while
오늘 수업
package days01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.jdbc.driver.OracleDriver;
/**
* @author k≡n¡k
* @date 2022. 5. 2. - 오후 3:13:27
* @subject Connection 객체를 사용해서 DBMS(오라클) 연결/닫기
* @content DBMS 연결(Connection)하는 순서 *** (암기)
*/
public class Ex02 {
public static void main(String[] args) {
// 연결문자열
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "SCOTT";
String password = "tiger"; // 비밀번호는 대소문자 구분한다.
try {
// 1. Class.forName() JDBC드라이버 로딩
// jdbcPro 자바 프로젝트 안에 ojdbc6.jar 참조
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. DriverManager.getConnection() 로 Connection 객체를 얻어온다.
Connection conn = DriverManager.getConnection(url, user, password);
// 3. 필요한 작업 ( CRUD ) X
System.out.println( conn );
System.out.println( conn.isClosed() ); // boolean [false]/true
// 4. 연결종료 ( Connection 객체 close )
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(" END ");
} //
} //
// 1. 에러 메시지 : java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
// 2. 에러 메시지 : java.sql.SQLException: No suitable driver found for jdbc.oracle.thin:@localhost:1521:xe
package days01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author k≡n¡k
* @date 2022. 5. 2. - 오후 3:55:04
* @subject DB 연동 + 필요한 작업( CRUD )
* @content 4:06 수업 시작
* dept테이블의 부서 정보를 조회
*/
public class Ex03 {
public static void main(String[] args) {
// DB 연동 순서
String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "SCOTT";
String password = "tiger";
Connection conn = null;
String sql = "SELECT * FROM dept";
/*
Connection conn을 사용해서 생성한다.
ㄱ. Statement 기본 객체
ㄴ. PreparedStatement ? 바인딩 변수를 사용해서 쿼리 실행 객체
ㄷ. CallableStatement 저장 프로시저를 호출해서 쿼리 실행 객체
*/
Statement stmt = null;
try {
// 1. Class.forName() JDBC Driver 로딩 + (ojdbc6.jar참조)
Class.forName(className);
// 2. DriverManger.getConnection() 커넥션 객체를 얻어온다 + 연결문자열( url, user, password )
conn = DriverManager.getConnection(url, user, password);
// 3. 필요한 작업 - CRUD
stmt = conn.createStatement();
/*
true rs.next() --> 10 ACCOUNTING NEW YORK
true rs.next() --> 20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
*/
ResultSet rs = stmt.executeQuery(sql); // SELECT ( DQL문 )
// stmt.executeUpdate(sql); // INSERT, UPDATE, DELETE ( DML 문)
// boolean rs.next() 리절트셋 안에 다음 레코드가 있니? true/ false
if( rs.next() ) {
// 다음(next) 레코드를 읽어와서 출력.
int deptno = rs.getInt(1);
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc);
}else {
System.out.println(" 읽을 레코드는 없습니다. ");
}
// boolean rs.next() 리절트셋 안에 다음 레코드가 있니? true/ false
if( rs.next() ) {
// 다음(next) 레코드를 읽어와서 출력.
int deptno = rs.getInt(1);
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc);
}else {
System.out.println(" 읽을 레코드는 없습니다. ");
}
// boolean rs.next() 리절트셋 안에 다음 레코드가 있니? true/ false
if( rs.next() ) {
// 다음(next) 레코드를 읽어와서 출력.
int deptno = rs.getInt(1);
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc);
}else {
System.out.println(" 읽을 레코드는 없습니다. ");
}
// boolean rs.next() 리절트셋 안에 다음 레코드가 있니? true/ false
if( rs.next() ) {
// 다음(next) 레코드를 읽어와서 출력.
int deptno = rs.getInt(1);
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc);
}else {
System.out.println(" 읽을 레코드는 없습니다. ");
}
// boolean rs.next() 리절트셋 안에 다음 레코드가 있니? true/ false
if( rs.next() ) {
// 다음(next) 레코드를 읽어와서 출력.
int deptno = rs.getInt(1);
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc);
}else {
System.out.println(" 읽을 레코드는 없습니다. ");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
// 4. close
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} //try
} // finally
System.out.println("= END =");
} //
} //
package days01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author k≡n¡k
* @date 2022. 5. 2. - 오후 3:55:04
* @subject DB 연동 + 필요한 작업( CRUD )
* @content 4:06 수업 시작
* dept테이블의 부서 정보를 조회
* 문제) sql 쿼리의 실행결과가 한 개의 레코드도 없다면
* "레코드가 존재하지 않습니다." 라고 출력.
*/
public class Ex03_02 {
public static void main(String[] args) {
// DB 연동 순서
String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "SCOTT";
String password = "tiger";
Connection conn = null;
String sql = "SELECT * FROM dept";
//String sql = "SELECT * FROM dept WHERE deptno = 90";
/*
Connection conn을 사용해서 생성한다.
ㄱ. Statement 기본 객체
ㄴ. PreparedStatement ? 바인딩 변수를 사용해서 쿼리 실행 객체
ㄷ. CallableStatement 저장 프로시저를 호출해서 쿼리 실행 객체
*/
Statement stmt = null;
try {
// 1. Class.forName() JDBC Driver 로딩 + (ojdbc6.jar참조)
Class.forName(className);
// 2. DriverManger.getConnection() 커넥션 객체를 얻어온다 + 연결문자열( url, user, password )
conn = DriverManager.getConnection(url, user, password);
// 3. 필요한 작업 - CRUD
stmt = conn.createStatement();
/*
true rs.next() --> 10 ACCOUNTING NEW YORK
true rs.next() --> 20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
*/
ResultSet rs = stmt.executeQuery(sql); // SELECT ( DQL문 )
// stmt.executeUpdate(sql); // INSERT, UPDATE, DELETE ( DML 문)
// boolean rs.next() 리절트셋 안에 다음 레코드가 있니? true/ false
if ( rs.next() ) {
do{
// 다음(next) 레코드를 읽어와서 출력.
int deptno = rs.getInt(1);
String dname = rs.getString("dname");
String loc = rs.getString("loc");
System.out.printf("%d\t%s\t%s\n", deptno, dname, loc);
}while( rs.next() );// while
} else {
System.out.println("결과물이 한 개도 존재하지 않습니다.");
} // if
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
// 4. close
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} //try
} // finally
System.out.println("= END =");
} //
} //
'JDBC' 카테고리의 다른 글
[JDBC]days04 - 게시판 만들기 (DTO ,DAO) (0) | 2022.05.09 |
---|---|
[JDBC] days02- (조회,수정,삭제,검색) (0) | 2022.05.04 |
[JDBC] days02 - 클래스 select (0) | 2022.05.03 |
[JDBC] days02-db에 연동하기 위한 클래스 라이브러리 생성 (0) | 2022.05.03 |
이클립스 환경 설정 (0) | 2022.05.02 |
Comments