작성
·
951
0
강의를 듣다가 데이터베이스를 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);
}
}
어떤 부분을 추가하고 수정해야하는지 잘 모르겠습니다 ㅠ