public class DBConnectionTest {
public static void main(String[] args) throws Exception {
String DB_URL = "jdbc:mysql://localhost:3306/springbasic?useUnicode=true&characterEncoding=utf8";
→ mysql(jdbc:mysql://[host]:[port]/[database]?useUnicode=true&characterEncoding=utf8)
String DB_URL = "jdbc:oracle:thin:@localhost:1521:orcl";
→ oracle(jdbc:oracle:thin:@[host]:[port]:[SID])
String DB_URL = "jdbc:postgresql://localhost:5432/springbasic";
→ postgresql(jdbc:postgresql://[host]:[port]/[database])
// 여기선 mysql사용
String DB_USER = "아이디";
String DB_PASSWORD = "패스워드";
Connection conn = DriverManager.getConnection(DB_URL , DB_USER , DB_PASSWORD);
Statement stmt = conn.createStatement();
String query = "SELECT NOW()";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
String curDate = rs.getString(1);
System.out.println();
}
}
}
이러고 실행하면?
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/springbasic?useUnicode=true&characterEncoding=utf8
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at com.fastcampus.ch3.DBConnectionTest.main(DBConnectionTest.java:17)
이런 오류가 나오는데
1. JDBC 드라이버 매니저가 로드되지 않았거나
2. 클래스패스에 JDBC 드라이버가 없어서
해당 DB에(MySQL) 연결할수 없다는 오류이고 MySQL만 설치해두고 워크벤치에서 돌아가는 것만 확인 했으니 Maven dependency를 추가해야한다.
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
pom.xml에 추가 후 메이븐 업데이트를 하고 다시 실행 해보면 잘나온다.