MCPcopy Create free account
hub / github.com/castello/spring_basic / DBConnectionTest

Class DBConnectionTest

ch3/DBConnectionTest.java:5–26  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3import java.sql.*;
4
5public class DBConnectionTest {
6 public static void main(String[] args) throws Exception {
7 // 스키마의 이름(springbasic)이 다른 경우 알맞게 변경해야 함
8 String DB_URL = "jdbc:mysql://localhost:3306/springbasic?useUnicode=true&characterEncoding=utf8";
9
10 // DB의 userid와 pwd를 알맞게 변경해야 함
11 String DB_USER = "asdf";
12 String DB_PASSWORD = "1234";
13
14 Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); // 데이터베이스의 연결을 얻는다.
15 Statement stmt = conn.createStatement(); // Statement를 생성한다.
16
17 String query = "SELECT now()"; // 시스템의 현재 날짜시간을 출력하는 쿼리(query)
18 ResultSet rs = stmt.executeQuery(query); // query를 실행한 결과를 rs에 담는다.
19
20 // 실행결과가 담긴 rs에서 한 줄씩 읽어서 출력
21 while (rs.next()) {
22 String curDate = rs.getString(1); // 읽어온 행의 첫번째 컬럼의 값을 String으로 읽어서 curDate에 저장
23 System.out.println(curDate); // 2022-01-11 13:53:00.0
24 }
25 } // main()
26}
27
28/* [실행결과]
292022-01-11 13:53:00.0

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected