-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBUtils.java
More file actions
49 lines (42 loc) · 1.34 KB
/
DBUtils.java
File metadata and controls
49 lines (42 loc) · 1.34 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
package com.koreait.board3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {
public static void main(String[] args) {
try {
getCon();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getCon() throws Exception {
final String DRIVER = "com.mysql.cj.jdbc.Driver";
final String URL = "jdbc:mysql://localhost:3308/boardv3";
final String USER_NAME = "root";
final String PASSWORD = "koreait";
Class.forName(DRIVER);
Connection con=DriverManager.getConnection(URL,USER_NAME,PASSWORD);
System.out.println("연결 성공!");
return con; //행의 개수를 return 받음
}
public static void close(Connection con,PreparedStatement ps) {
close(con,ps, null);
}
//select때만 rs로 받는다.
public static void close(Connection con,PreparedStatement ps, ResultSet rs) {
if(rs!=null) {
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
}
if(ps!=null) {
try {ps.close();} catch (SQLException e) {e.printStackTrace();}
}
if(con!=null) {
try {con.close();} catch (SQLException e) {e.printStackTrace();}
}
//안 닫길 거라서 따로 따로 해준다..?
}
}