๐jdbcexam
|_ ๐kr.or.connect.jdbcexam
|_ ๐kr.or.connect.jdbcexam.dao
|_ ๐kr.or.connect.jdbcexam.dto
Role ๊ฐ์ฒด์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๋๋ค
package kr.or.connect.jdbcexam.dto;
public class Role {
private Integer roleId;
private String description;
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 + "]";
}
}
DB์ connectํ๋ ๋ถ๋ถ์ ๋ด๋นํจ
์๋์ ๊ฐ์ ํจ์๋ค์ ์ ์ํ์ฌ DB๋ฅผ ์ฝ๊ณ ์ฐ๊ฑฐ๋ ์์ ํ ์ ์๋ค!
๋๋ต์ ์ธ ์คํ๊ณผ์
1. conn ๊ฐ์ฒด๋ฅผ ์์ฑ
2. prepare Statement ๋ณ์๋ฅผ ์์ฑํด์ ์ ์ธ๋ฌธ์ ์ํ๋ ๊ฐ ๋์ ์ผ๋ก ์ ๋ ฅํ ์ ์๋๋ก ํจ
3. prepare Statement ๋ณ์์ ์ ์ฅ๋ ์ฟผ๋ฆฌ๋ฅผ ์คํ์์ผ next()ํจ์๋ก ํ์ค์ฉ ๊ฐ์ ธ์ค๋๋ก ํจ
4. rs๊ฐ null์ผ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ -> close
5. ps๊ฐ null์ผ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ -> close
6. conn์ด null์ผ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ -> close
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";
private static String dbUser = "connectuser";
private static String dbpasswd = "connect123!@#";
public Role getRole(Integer roleId) {
Role role = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
// preparedStatement๊ฐ ๊ฐ์ง๊ณ ์๋ ๋ฌผ์ํ์ ๋ํ ๊ฐ์ ์ง์ ๊ฐ๋ฅ
String sql = "SELECT description, role_id FROM role WHERE role_id = ?";
ps = conn.prepareStatement(sql); // sql๋ฌธ์ ์คํ์ํค๋๋ก ํจ
ps.setInt(1, roleId);//1๋ฒ์งธ ๋ฌผ์ํ์ roleId ๋ณ์๋ฅผ ์ ์ฉ
rs = ps.executeQuery();
if(rs.next()) {
String description = rs.getString(1); // ์ฒซ๋ฒ์งธ column์ ๊ฐ ๊ฐ์ ธ์ด
int id = rs.getInt(2); //column์ด ๊ฐ์ง ๊ฐ ๊ฐ์ ธ์ด
role = new Role(id, description);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if(rs != null) { // rs๊ฐ null์ผ ๋๋ ์ํ๋์ง ์๋๋ก ํจ
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps != null) { // ps๊ฐ null์ผ ๋๋ ์ํ๋์ง ์๋๋ก ํจ
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null) { // conn๊ฐ null์ผ ๋๋ ์ํ๋์ง ์๋๋ก ํจ
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return role;
}
}
RoleDao์ getํจ์๋ฅผ ์ฌ์ฉํด DB์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์ธ ํด๋์ค
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); #role_id ๊ฐ 100์ธ ๋ ์ฝ๋๋ฅผ ๊ฐ์ ธ์ด
System.out.println(role);
}
}