-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionExam4.java
More file actions
51 lines (40 loc) · 1.46 KB
/
ExceptionExam4.java
File metadata and controls
51 lines (40 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package sec06.ch08;
import java.sql.*;
public class ExceptionExam4 {
public static void main(String[] args) {
//싱글톤이란 프로그램 안에서 객체 1개만 만들어지도록 유도
//2개 이상 만들 수 없도록 함
DbUtils du=DbUtils.getInstance(); //static을 받은 거니까
// DbUtils du2=DbUtils.getInstance();
Connection con=du.getCon();
// System.out.println(du==du2); 주소 값 달라서 안 나옴
}
}
//외부에서 객체화되는 거 막기
class DbUtils{
private DbUtils() {} //기본 생성자 막는 건 private
private static DbUtils dbutils = new DbUtils();
//개발자들은 = 붙이는 거 싫어한대..
public static DbUtils getInstance() {
// if(dbutils==null) {
// dbutils=new DbUtils();
// }
return dbutils;
} //객체 1개 생성
Connection getCon(){
Connection con=null; // 지역변수이기 때문에 null 안 넣어주면 에러 뜸
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//jdbc:mysql://ip주소:포트번호/데이터베이스명
final String URL="jdbc:mysql://localhost:3308/java";
final String USERNAME="root";
final String PASSWORD="koreait";
con=DriverManager.getConnection(URL,USERNAME,PASSWORD);
System.out.println("접속 성공!");
}catch (Exception e) {
e.printStackTrace();
System.out.println("접속 실패!");
} //오른쪽 static 메소드(객체화를 안 하니까)
return null;
}
}