목표 : role 테이블의 description을 변경
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 int updateRole(String old_description, String new_description) {
int updateCount = 0;
Connection conn = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbUser, dbpasswd);
String sql = "UPDATE role SET description = ? WHERE description = ?";
ps = conn.prepareStatement(sql);
ps.setString(2, old_description);
ps.setString(1, new_description);
updateCount = ps.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (Exception ex) {
}
} // if
if (conn != null) {
try {
conn.close();
} catch (Exception ex) {
}
} // if
} // finally
return updateCount;
}
}
package kr.or.connect.jdbcexam;
import kr.or.connect.jdbcexam.dao.RoleDao;
public class JDBCExam3 {
public static void main(String[] args) {
RoleDao dao = new RoleDao();
int updateCount = dao.updateRole("CTO", "CEO");
System.out.println(updateCount);
}
}
결과