BKJang / do-you-know-backend

๐Ÿ–ฅ This repository contains contents about overall knowledge of backend
https://bkjang.github.io/do-you-know-backend
2 stars 0 forks source link

JDBC #5

Open BKJang opened 4 years ago

BKJang commented 4 years ago

๐Ÿ™ Reference

BKJang commented 4 years ago

JDBC(Java Database Connectivity)

JDBC๋Š” JAVA๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ์ ‘์†๊ณผ SQL ์‹คํ–‰ ๋ฐ ๊ทธ์— ๋”ฐ๋ฅธ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ์˜ ํ•ธ๋“ค๋ง์„ ์ œ๊ณตํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ ˆ์ฐจ์— ๋Œ€ํ•œ ๊ทœ์•ฝ์ด๋‹ค. ์ฆ‰, ์‰ฝ๊ฒŒ ๋งํ•ด JAVA๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ SQL๋ฌธ์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ API๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

JDBC ํด๋ž˜์Šค ์ƒ์„ฑ

JDBC_CLASS_CONSTRUCTION

In Maven Dependency

<dependency>   
  <groupId>mysql</groupId>   
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.45</version>
 </dependency>

Import

import java.sql.*;

Load Driver

Class.forName( "com.mysql.jdbc.Driver" );

Get Connection

String dburl  = "jdbc:mysql://localhost/dbName";

Connection con =  DriverManager.getConnection ( dburl, ID, PWD );
public static Connection getConnection() throws Exception{
    String url = "jdbc:oracle:thin:@117.16.46.111:1521:xe";
    String user = "smu";
    String password = "smu";
    Connection conn = null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection(url, user, password);
    return conn;
}

Construct Statement

Statement stmt = con.createStatement();

Execute Query

ResultSet rs = stmt.executeQuery("select no from user" );

stmt.execute(โ€œqueryโ€); //any SQL
stmt.executeQuery(โ€œqueryโ€); //SELECT
stmt.executeUpdate(โ€œqueryโ€); //INSERT, UPDATE, DELETE

Get Result with ResultSet

ResultSet rs = stmt.executeQuery( "select no from user" );
while (rs.next())
  System.out.println(rs.getInt( "no"));

Close Connection

rs.close();

stmt.close();

con.close();

Example

// GET
public List<GuestBookVO> getGuestBookList(){
  List<GuestBookVO> list = new ArrayList<>();
  GuestBookVO vo = null;
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;

  try {
    conn = DBUtil.getConnection();
    String sql = "select * from guestbook";
    ps = conn.prepareStatement(sql);
    rs = ps.executeQuery();
    while(rs.next()){
      vo = new GuestBookVO();
      vo.setNo(rs.getInt(1));
      vo.setId(rs.getString(2));
      vo.setTitle(rs.getString(3));
      vo.setConetnt(rs.getString(4));
      vo.setRegDate(rs.getString(5));
      list.add(vo);
    }
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    DBUtil.close(conn, ps, rs);
  }     
  return list;      
}
// ADD
public int addGuestBook(GuestBookVO vo){
  int result = 0;
  Connection conn = null;
  PreparedStatement ps = null;

  try {
    conn = DBUtil.getConnection();
    String sql = "insert into guestbook values("
        + "guestbook_seq.nextval,?,?,?,sysdate)";
    ps = conn.prepareStatement(sql);
    ps.setString(1, vo.getId());
    ps.setString(2, vo.getTitle());
    ps.setString(3, vo.getConetnt());
    result = ps.executeUpdate();
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    DBUtil.close(conn, ps);
  }

  return result;
}
// CLOSE
public static void close(Connection conn, PreparedStatement ps){
  if (ps != null) {
    try {
      ps.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
  if (conn != null) {
    try {
      conn.close();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}