오라클 DB연결 bean설정하는 방법 질문합니다.
1029
작성한 질문수 4
강의를 듣다가 데이터베이스를 h2 말고 oracle로 사용하고싶어서 변경해봤는데 방법을 잘 모르겠습니다..
단순하게 db 연결은 가능합니다.
public class JdbcTest01 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "이름", "비밀번호");
Class.forName("oracle.jdbc.driver.OracleDriver");
String sql = "select * from member";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println(" == 쿼리문 처리 결과 ==");
while (rs.next()) {
System.out.println("Lprod_id : " + rs.getInt("id"));
System.out.println("------------------------------");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
} finally {
if (rs!=null) {
try {
rs.close();
} catch (SQLException e2) {
}
}
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e2) {
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException e2) {
}
}
}
}
}
그런데.. 빈 설정하는 방법을 잘 모르는 것 같습니다 ㅠㅠ
(.getConnection에 유저이름과 비밀번호는 제대로 설정했습니다!
밑에만 바꿨어요!)
public class JdbcMemberRepository implements MemberRepository {
private final DataSource dataSource;
public JdbcMemberRepository(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public Member save(Member member) {
String sql = "insert into member(name) values(?)";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "이름", "비밀번호");
pstmt = conn.prepareStatement(sql,
Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, member.getName());
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
if (rs.next()) {
member.setId(rs.getLong(1));
} else {
throw new SQLException("id 조회 실패");
}
return member;
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
// 5. 자원 반납
if (rs!=null) {
try {
rs.close();
} catch (SQLException e2) {
// TODO: handle exception
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException e2) {
// TODO: handle exception
}
}
}
}프로퍼티스
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.username=이름
spring.datasource.password=비밀번호
디펜던시스
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.oracle.ojdbc:ojdbc8:19.3.0.0'
implementation 'com.oracle.database.jdbc:ojdbc6:11.2.0.4'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.h2database:h2'
}
@Configuration
@Configuration
public class SpringConfig {
private DataSource dataSource;
public SpringConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean //스프링빈수동등록
public MemberService memberService() {
return new MemberService((MemoryMemberRepository) memberRepository());
}
@Bean
public MemberRepository memberRepository() {
//return new MemoryMemberRepository();
return new JdbcMemberRepository(dataSource);
}
}
어떤 부분을 추가하고 수정해야하는지 잘 모르겠습니다 ㅠ
답변 1
..
0
78
2
Unused property.....
0
102
2
project JDK is misconfigured
0
133
2
외부 API의 ID 타입(String/UUID)과 내부 도메인의 ID 타입(Long)이 불일치할 때의 설계 정석
0
83
2
단위/통합 테스트 버전충돌 문제
0
91
2
❗️springboot 4.0.2 버전 aspectj dependency 설정❗️
0
231
1
왜 컨트롤러는 변한게 없는데 새로 만든 html 파일이 뜨나요?
0
104
2
윈도우 build test오류 질문
1
103
2
테스트 관련 공부에 대한 조언을 얻고 싶습니다
0
101
2
테스트 실행 시 에러 질문
0
292
1
name을 통한 비교와 객체를 통한 비교
0
77
1
빌드 후 libs 없음
1
130
1
윈도우 gradlew.bat 에러
0
168
1
@PostMapping("/members/new")가 동작하지 않습니다
0
90
1
java static class와 kotlin class
0
79
1
스프링 DB연결
0
127
1
소요 시간
0
87
2
ddl.sql에 빨간 밑줄
1
102
2
welcome page 에러
0
183
3
잘 모르겠습니다.
0
164
2
fail을 똑같이 쳤는데 오류가 발생해요
0
158
2
index.html Welcome page
0
125
1
프로젝트 gradle-groovy ?
0
372
1
테스트코드 메서드명 한글
0
204
2





