Jsp - DB(Mysql)와 연동

2020. 8. 2. 11:05Java/JSP

반응형

localhost에 있는 db에 연결/ 연결을 원하는 테이블명을 넣어서 작성한다.

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

String url = "jdbc:mysql://localhost/테이블명?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&validationQuery=select 1&characterEncoding=UTF-8";
		
			con = DriverManager.getConnection(url,DBID,DBpwd);

다음과 같이 연결 정보를 리턴해준다.

public static Connection getConnection() {
		Connection con=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost/jsptest?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&validationQuery=select 1&characterEncoding=UTF-8";
		
			con = DriverManager.getConnection(url,"root","ggthegame2");
			System.out.println("DB 연결 성공");
		}catch(Exception e) {
			System.out.println(e);
		}
		return con;
	}

 

반응형

'Java > JSP' 카테고리의 다른 글

Jsp Post,get 한글전송  (0) 2020.08.02
Jsp - session 저장(로그인 상태 유지하기)  (0) 2020.08.02
Jsp - Post 전송  (0) 2020.08.02