Java/JSP

07.07 JDBC와 JSP연동

amungstudy 2023. 7. 7. 12:35

Data Source Explorer에 mysql 연결

Driver Class : com.mysql.cj.jdbc.Driver 로 설정해야함 주의.

그냥 해도 되지만 거쳐서 오는 것임. 

 

프로젝트 별로 WEB-INF/lib에 커넥터J 자르파일 넣어줘서 DB설정 가능.

C:\workspace\jsp\tomcat\lib 여기에 넣으면 서버 공용으로 사용 가능.

 


항상 DB 사용 시 연결이 잘 되어있는지 확인해야한다.

// database와 연결하기 위한 필수 정보

// 현재 프로젝트 또는 서버에 jdbc 라이브러리가 정상적으로 등록되어 있는지 확인하기 위한 정보

String driver = "com.mysql.cj.jdbc.Driver";

// db server와 연결하기 위한 위치 정보

String url = "jdbc:mysql://localhost:3306/digital_jsp";

// 권한을 가진 사용자 계정 정보

String username = "digital";

String password = "12345";

 

Connection conn = null;

 

try{

Class.forName(driver);

out.println("Driver Class가 존재합니다.");

conn = DriverManager.getConnection(url,username,password);

out.println("DB 연결 완료");

out.println(conn);

}catch(ClassNotFoundException e){

out.println("Driver Class를 찾을 수 없습니다.");

}catch(SQLException e){

out.println("연결 요청 정보 오류 : "+ e.getMessage());

}finally{

if(conn != null){

conn.close();

}

}