실습1 - select
프로젝트 세팅
- Maven Project(maven-archetype-quickstart 1.1)
- pom.xml 의존성 추가
<!-- 자신의 버전에 맞는 mysql 의존성 주입 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- jdk 1.8 사용하기 위해 plugins 추가 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
프로젝트 실습
목표 : role 테이블의 데이터를 select로 읽어오기
- Role.java 코드작성
package kr.or.connect.jdbcexam.dto;
// Source의 Generate를 통해 getter, setter와 toString()을 쉽게 생성할 수 있다.
public class Role {
private Integer roleId;
private String description;
public Role() {
}
public Role(Integer roleId, String description) {
super();
this.roleId = roleId;
this.description = description;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Role [roleId=" + roleId + ", description=" + description + "]";
}
}
- RoleDao.java 코드작성
package kr.or.connect.jdbcexam.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import kr.or.connect.jdbcexam.dto.Role;
public class RoleDao {
private static String dburl = "jdbc:mysql://localhost:3306/connectdb?serverTimezone=UTC";
private static String dbUser = "connectuser";
private static String dbpasswd = "PASSWORD";
public Role getRole(Integer roleId) {
Role role = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
String sql = "SELECT role_id, description FROM role WHERE role_id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, roleId); // arg1 : ?의 순서. arg2 : ?의 값
rs = ps.executeQuery();
if(rs.next()) {
int id = rs.getInt("role_id");
String description = rs.getString(2);
role = new Role(id, description);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return role;
}
}
- 실행코드작성
package kr.or.connect.jdbcexam;
import kr.or.connect.jdbcexam.dao.RoleDao;
import kr.or.connect.jdbcexam.dto.Role;
public class JDBCExam1 {
public static void main(String[] args) {
RoleDao dao = new RoleDao();
Role role = dao.getRole(100);
System.out.println(role);
}
}
결과
Author And Source
이 문제에 관하여(실습1 - select), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@oyeon/JDBC-실습1-select저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)