๐ RoleDao.java๋ MySQL๊ณผ Role.java ํด๋์ค๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ํด๋์ค ์ด๋ค.
JDBC API ์ฌ์ฉ์ ์ํด Class.forName
๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ JDBC ๋๋ผ์ด๋ฒ ๋ก๋ฉ
DB ๋ง๋ค ํด๋์ค ์ด๋ฆ์ด ๋ค๋ฅด๋ฏ๋ก ํด๋น DB ์ด๋ฆ์ ์ ํํ๊ฒ ๋ฃ์ด์ผ ํ๋ค.
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์
//๋๋ผ์ด๋ฒ ๋ก๋ฉ
Class.forName("com.mysql.jdbc.Driver");
//์ปค๋ฅ์
๊ฐ์ฒด
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
//delete ๋ ๊ฒฐ๊ณผ ๊ฑด์๋ฅผ ๋ํ๋ผ int ํ ๋ณ์ ์ ์ธ
int deleteCount = 0;
String sql = "delete from role where role_id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, roleId);
deleteCount = ps.executeUpdate();
๐ role ํ ์ด๋ธ์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅํด๋ณด์.
public class JDBCexam4 {
public static void main(String[] args) {
int roleId = 500;
RoleDao dao = new RoleDao();
int deleteCount = dao.deleteRole(roleId);
List<Role> list = dao.getRoles();
System.out.println(deleteCount);
for(Role role : list) {
System.out.println(role);
}
}
}
//update ๋ ๊ฒฐ๊ณผ ๊ฑด์๋ฅผ ๋ํ๋ผ int ํ ๋ณ์ ์ ์ธ
int updateCount = 0;
String sql = "update role set description = ? where role_id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, role.getDescription());
ps.setInt(2, role.getRoleId());
updateCount = ps.executeUpdate();
๐ role = 500์ธ ๋ฐ์ดํฐ์ description ๊ฐ์ "CEO"๋ก update ํด๋ณด์.
public class JDBCexam5 {
public static void main(String[] args) {
int roleId = 500;
String description = "CEO";
Role role = new Role(roleId, description);
RoleDao dao = new RoleDao();
int updateCount = dao.updateRole(role);
System.out.println(updateCount);
//์๋ง๊ฒ ๋ฐ์ดํฐ ์์ ๋์๋์ง ํ์ธํ๊ธฐ
List<Role> list = dao.getRoles();
for(Role rolelist : list) {
System.out.println(rolelist);
}
}
}
๐ ์ ์ฒด ์ฝ๋
RoleDao.java - deleteRole()
//Delete
public int deleteRole(Integer roleId) {
int deleteCount = 0;
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
String sql = "delete from role where role_id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, roleId);
deleteCount = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
return deleteCount;
}
JDBCexam4.java
public class JDBCexam4 {
public static void main(String[] args) {
int roleId = 500;
RoleDao dao = new RoleDao();
int deleteCount = dao.deleteRole(roleId);
List<Role> list = dao.getRoles();
System.out.println(deleteCount);
for(Role role : list) {
System.out.println(role);
}
}
}
RoleDao.java - updateRole()
//Update
public int updateRole(Role role) {
int updateCount = 0;
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
String sql = "update role set description = ? where role_id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, role.getDescription());
ps.setInt(2, role.getRoleId());
updateCount = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
return updateCount;
}
JDBCexam5.java
public class JDBCexam5 {
public static void main(String[] args) {
int roleId = 500;
String description = "CEO";
Role role = new Role(roleId, description);
RoleDao dao = new RoleDao();
int updateCount = dao.updateRole(role);
System.out.println(updateCount);
//์๋ง๊ฒ ๋ฐ์ดํฐ ์์ ๋์๋์ง ํ์ธํ๊ธฐ
List<Role> list = dao.getRoles();
for(Role rolelist : list) {
System.out.println(rolelist);
}
}
}