JDBC
by 핫도구반응형
JAVA와 MYSQL을 연결하기 위해서 .jar 형식의 파일을 사용해서 연결을 해야한다.
필자가 사용하고 있는 버전에 맞게 https://downloads.mysql.com/archives/c-j/ 에 들어가서 파일을 다운로드 해주고 사용하고 있는 주 폴더에 넣어준다.
이후 eclipse 실행 후 사용할 javaproject에 우클릭 한 후 build path →configure build path -> libraries -> classpath클릭 후 add External jars 를 통해 다운로드한 .jar형식의 파일을 넣어준다.
이후 새로운 클래스(DBConnection)를 만들어 SQL과 연결을 해준다.
import java.sql.Connection;
public class DBConnection {
public static Connection getConnection() {
try{
class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://@127.0.0.1:3306/mydb?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true";
Connection cn = DriverManager.getConnection(url, "root", "mysql");
System.out.println("JDBC Connection Success");
return cn;
} catch {
System.out.println("JDBC Connection Exception" + e.toString());
return null;
}
}
}
을 활용해 필자의 mysql과 연결할 수 있도록 url을 적용하고 이 url에는 port번호와 mysql을 실행하지 않아도 적용이 될 수 있도록 하는 구문이 적용되어 있다. 또한 sql 이름과 비밀번호를 cn에 전달한다.
이를 통해 JAVA와 MYSQL을 연결할 수 있다.
반응형
'JAVA > OLD' 카테고리의 다른 글
| PageFlow (0) | 2024.08.16 |
|---|---|
| DTO, DAO, SERVICE, CONTROLLER (0) | 2024.08.09 |
| Properties (0) | 2024.07.29 |
| MapIterator (0) | 2024.07.29 |
| Map(HashMap, TreeMap, LinkedHashMap) (0) | 2024.07.29 |
블로그의 정보
AquaMan
핫도구