ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

DBUtil

๐Ÿฉท ์Šˆ ๐Ÿงก 2010. 6. 15. 21:54
728x90
SMALL
package util;
import java.sql.*;
import java.io.*;
import java.util.*;

public class DBUtil {
private static String driver, url, user, pass;
//๋ณดํ†ต ์ด๋Ÿฐ ์ •๋ณด๋“ค์€ ํ•œ๋ฒˆ ์ฝ์œผ๋ฉด ์ž์ฃผ๋ณ€๊ฒฝ๋˜์ง€๋Š” ์•Š๋Š”๋‹ค.
//์ด๋Ÿด๊ฒฝ์šฐ ํ•œ๋ฒˆ ์ฝ์–ด์„œ ๊ณ„์† ์žฌํšจใ…‘ใ„น์šฉ ๋ผํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค.
// ํ•œ๋ฒˆ ์ฝ์–ด์„œ ์ญˆ~ ์ด๋Ÿฐ์ž‘์—…์ด ์žˆ๋‹ค๋ฉด
// static ํ‚ค์›Œ๋“œ๋ฅผ ๋– ์˜ฌ๋ฆฌ์ž.
static {
loadInfo();
driverLoading();
}

private static void loadInfo() {
// TODO Auto-generated method stub
try {
Properties props;
File file = new File(System.getProperty("user.dir"), "db.properties");
FileInputStream fis;
fis = new FileInputStream("db.properties");
props = new Properties();
props.load(fis);
url = props.getProperty("url");
user = props.getProperty("user");
pass = props.getProperty("pass");
driver = props.getProperty("driver");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static void driverLoading()  {
// TODO Auto-generated method stub
//jdbc 1 driver loading
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, pass);
}
}



##############################################################
# db.properties ํŒŒ์ผ
##############################################################
#=======================================================
# # is comment
# db setting
#=======================================================

url = jdbc:mysql://localhost:3306/test
user = javamaster
pass = 1111
driver = com.mysql.jdbc.Driver
728x90
LIST
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2025/10   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
๊ธ€ ๋ณด๊ด€ํ•จ
250x250