๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

by Leica 2020. 2. 9.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ


[IntelliJ] JAVA ์„œ๋ธ”๋ฆฟ + MySQL ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

 

ํ™˜๊ฒฝ ์ •๋ณด

IntelliJ IDEA 2019.3 Ultimate, Apache Tomcat 9, MySQL 8

 

์œ„์™€ ๊ฐ™์€ ํ•™์ƒ ํ…Œ์ด๋ธ”์„ ์ด์šฉํ•ด ํ•™์ƒ ๋ชฉ๋ก ์ถœ๋ ฅ, ์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก, ๊ธฐ์กด ํ•™์ƒ ์ •๋ณด ์ˆ˜์ •์„ ํ•˜๋Š” ์„œ๋ธ”๋ฆฟ์„ ๊ตฌํ˜„ํ•œ๋‹ค.

 

1. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ๋ฐ ์„ธํŒ…

์šฐ์„  ์›น ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

๊ธฐ๋ณธ Java ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒํ•˜๊ณ  Web Application, WebServices์— ์ฒดํฌํ•œ๋‹ค.

Version์€ Apache Axis๋ฅผ ์„ ํƒํ•œ๋‹ค.

Libraries๋Š” Set up library later๋ฅผ ์„ ํƒํ•˜๊ณ  [Next]๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

ํ”„๋กœ์ ํŠธ ์ด๋ฆ„๊ณผ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜๊ณ  [Finish]๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

ํ”„๋กœ์ ํŠธ๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์œผ๋ฉด jarํŒŒ์ผ์„ ์ €์žฅํ•  lib ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์›ํ•˜๋Š” ์œ„์น˜์— ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ์•ˆ์— MySQL JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋„ฃ๋Š”๋‹ค.

์ฐธ๊ณ ๋กœ MySQL JDBC ๋“œ๋ผ์ด๋ฒ„ ํŒŒ์ผ์€ ์„ค์น˜ ๋””๋ ‰ํ† ๋ฆฌ/Connector J x.x ๋””๋ ‰ํ† ๋ฆฌ ํ•˜์œ„์— ์žˆ๋‹ค.

MySQL JDBC ๋“œ๋ผ์ด๋ฒ„ ํŒŒ์ผ

 

๋‹ค์Œ์œผ๋กœ Project Structure ์ฐฝ์„ ๋„์šด๋‹ค.

Dependencies - [+] - Library... ๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

ํ†ฐ์บฃ ์„œ๋ฒ„๋ฅผ ์„ ํƒํ•˜๊ณ  [Add Selected]๋ฅผ ํด๋ฆญํ•œ๋‹ค.

IntelliJ ํ†ฐ์บฃ ์—ฐ๋™์€ ๋‹ค์Œ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•œ๋‹ค.

[JAVA ์›น ๊ฐœ๋ฐœํ™˜๊ฒฝ] IntelliJ + Tomcat ์—ฐ๋™ ๋ฐฉ๋ฒ•

 

ํ†ฐ์บฃ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ ํ›„ ๋‹ค์‹œ [+] - JARs or directories...๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

์•„๊นŒ lib ํด๋”์— ์ถ”๊ฐ€ํ•œ JDBC ๋“œ๋ผ์ด๋ฒ„ jarํŒŒ์ผ์„ ์„ ํƒํ•˜๊ณ  [OK]๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

2. ์„œ๋ธ”๋ฆฟ ๊ตฌํ˜„

์ž๋ฐ” ์†Œ์Šคํด๋” ์•ˆ์— ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

 

@WebServlet("/student/list")
public class StudentListServlet2 extends GenericServlet {

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        Properties jdbcProperties = new Properties();
        jdbcProperties.setProperty("user", "username");
        jdbcProperties.setProperty("password", "****");
    }
}

GenericServlet์„ ์ƒ์†๋ฐ›๊ณ  service()๋ฅผ overrideํ•œ๋‹ค.

์„œ๋ธ”๋ฆฟ annotation @WebServlet์„ ์‚ฌ์šฉํ•ด์„œ "/student/list"๋ฅผ URL๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.

JDBC ์‚ฌ์šฉ์— ํ•„์š”ํ•œ 3๊ฐ€์ง€ ๊ฐ์ฒด Connection, Statement, ResultSet ์˜ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.

Properties ๊ฐ์ฒด์— user, password๋ฅผ ์„ค์ •ํ•œ๋‹ค. *๋กœ ์จ๋‘์—ˆ์ง€๋งŒ ์ ‘์†ํ•  ์‹ค์ œ username๊ณผ password๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

try {
    // 1. JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ
    // MySQL : com.mysql.cj.jdbc.Driver
    DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
} catch (SQLException e) {
    throw new ServletException(e);
} finally {
}

DriverManager.registerDriver()๋ฅผ ์ด์šฉํ•ด์„œ java.sql.Driver ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„์ฒด = JDBC ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•œ๋‹ค.

MySQL์€ com.mysql.cj.jdbc.Driver ํด๋ž˜์Šค๊ฐ€ Driver ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

Vendor๋‚˜ ๋ฒ„์ „์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ JDBC ๋“œ๋ผ์ด๋ฒ„ ๋ฌธ์„œ์—์„œ ํ•ด๋‹น ์ •๋ณด๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿšจ jdk1.6(jdbc4) ๋ถ€ํ„ฐ๋Š” getConnection ํ˜ธ์ถœ์‹œ Service Provider Mechanism์ด ์ ์šฉ๋˜์–ด ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ž๋™ ๋กœ๋“œ๋œ๋‹ค. ์ฆ‰ ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

try~catch ๋ธ”๋ก์œผ๋กœ ๋ฌถ์–ด SQLException์ด ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ServletException ์ธ์Šคํ„ด์Šค์— ๋‹ด์•„ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ(ํ†ฐ์บฃ)์—๊ฒŒ throwํ•˜๋„๋ก ํ•œ๋‹ค.

 

conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/knou", jdbcProperties);

DriverManager.getConnection()์„ ํ˜ธ์ถœํ•ด์„œ MySQL ์„œ๋ฒ„์— ์—ฐ๊ฒฐํ•œ๋‹ค.

์ฒซ ๋ฒˆ์งธ ์ธ์ž๊ฐ’์œผ๋กœ JDBC URL, ๋‘ ๋ฒˆ์งธ ์ธ์ž๊ฐ’์œผ๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ€ ๋‹ด๊ธด Properties ์ธ์Šคํ„ด์Šค๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

JDBC URL ํ˜•์‹์€ ๋“œ๋ผ์ด๋ฒ„๋งˆ๋‹ค ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๋‹ค.

DB ์—ฐ๊ฒฐ์— ์„ฑ๊ณตํ•˜๋ฉด java.sql.Connection ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

stmt = conn.createStatement();
rs = stmt.executeQuery(
        "SELECT STUDENT_NO, DEPARTMENT, STUDENT_NAME, GRADE, GENDER, AGE, PHONE_NUMBER, ADDRESS" +
                " FROM STUDENT" +
                " ORDER BY STUDENT_NO");

conn์„ ํ†ตํ•ด SQL๋ฌธ์„ ์„œ๋ฒ„์— ๋ณด๋‚ด์„œ ์‹คํ–‰ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

conn.createStatement()๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์€ java.sql.Statement ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด์ด๋‹ค.

Statement ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” DB์— ์งˆ์˜ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

SELECT๋ฌธ์„ ์‹คํ–‰ํ• ๋•Œ๋Š” executeQuery()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

executeQuery()๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ์ฒด๋Š” java.sql.ResultSet์˜ ๊ตฌํ˜„์ฒด์ด๋‹ค. ์ด ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด SELECT ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

servletResponse.setContentType("text/html; charset=UTF-8");
PrintWriter out = servletResponse.getWriter();
out.println("<html><head><title>ํ•™์ƒ ๋ชฉ๋ก</title></head>");
out.println("<body><h1>ํ•™์ƒ ๋ชฉ๋ก</h1>");
while (rs.next()) {
    out.println(
            rs.getString("STUDENT_NO") + ", " +
                    rs.getString("DEPARTMENT") + ", " +
                    rs.getString("STUDENT_NAME") + ", " +
                    rs.getInt("GRADE") + ", " +
                    rs.getString("GENDER") + ", " +
                    rs.getInt("AGE") + ", " +
                    rs.getString("PHONE_NUMBER") + ", " +
                    rs.getString("ADDRESS") + "<br>");
}
out.println("</body></html>");

๋ฐ์ดํ„ฐ๋ฅผ HTML๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

setContentType()์„ ํ˜ธ์ถœํ•ด์„œ ๋ฐ์ดํ„ฐ ํ˜•์‹์„ HTML๋กœ, ๋ฌธ์ž ์ง‘ํ•ฉ์„ UTF-8๋กœ ์ง€์ •ํ•œ๋‹ค.

next()์€ ์„œ๋ฒ„์—์„œ ๋‹ค์Œ ํ–‰์„ ๊ฐ€์ ธ์˜ค๋ฉฐ ๋‹ค์Œ ํ–‰์ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์ปฌ๋Ÿผ ๊ฐ’์„ ๊บผ๋‚ผ๋•Œ๋Š” getXXX()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. getXXX()์— ์ปฌ๋Ÿผ ์ด๋ฆ„ ํ˜น์€ ์ธ๋ฑ์Šค๋ฅผ ์ „๋‹ฌํ•ด์„œ ๊ฐ€์ ธ์˜จ๋‹ค.

์—ฌ๊ธฐ์—์„œ๋Š” ์ด๋ฆ„์„ ์ง€์ •ํ–ˆ๋‹ค.

 

๐Ÿšจ ์ปฌ๋Ÿผ ์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

 

finally {
    try {
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

finally ๋ธ”๋Ÿญ์—์„œ ์ž์›์„ ํ•ด์ œํ•œ๋‹ค.

์ž์›์„ ํ•ด์ œํ• ๋•Œ๋Š” ์—ญ์ˆœ์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค. (ResultSet -> Statement -> Connection)

 

3. ๋ฐฐ์น˜ ๋ฐ ํ…Œ์ŠคํŠธ

ํ†ฐ์บฃ ์‹คํ–‰ ํ™˜๊ฒฝ ์„ค์ • ์ฐฝ์„ ์—ด๊ณ  [+] - Artifact...๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

๋ชจ๋“ˆ์„ ์„ ํƒํ•˜๊ณ  [OK]๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

Application context๋ฅผ ์ง€์ •ํ•˜๊ณ  [OK]๋ฅผ ํด๋ฆญํ•œ๋‹ค.

 

ํ†ฐ์บฃ์„ ๊ธฐ๋™ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•œ๋‹ค.

 

์‹คํ–‰ ๊ฒฐ๊ณผ

 

ํ˜น์‹œ JDBC์—์„œ ClassNotFoundException์ด ๋ฐœ์ƒํ•œ๋‹ค๋ฉด

 

๋ชจ๋“ˆ Artifact์— JDBC ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ถ”๊ฐ€๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์—†๋‹ค๋ฉด ์ถ”๊ฐ€ํ•œ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ

 

#IntelliJ ์›นํ”„๋กœ์ ํŠธ

#JAVA DB ์—ฐ๋™ ์˜ˆ์ œ

#JAVA MySQL ์—ฐ๋™ ์˜ˆ์ œ

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€