?? util.java
字號:
/**
*
*/
package com.howeisoft.ums.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
* @author howei
*
*/
public class Util {
private static Properties cfg = new Properties();
static {
try {
InputStream in = Util.class.getClassLoader().getResourceAsStream(
"driver_data.properties");
cfg.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError();
}
}
public static Connection getConnection() {
if (cfg.getProperty("jdbc/MY_DS") == null) {
return getConnection_DM();
}
return getConnection_JNDI();
}
private static Connection getConnection_JNDI() {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/"
+ cfg.getProperty("jdbc/MY_DS"));
return ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("無法連接到 " + cfg.getProperty("jdbc/MY_DS"));
}
}
private static Connection getConnection_DM() {
try {
Class.forName(cfg.getProperty("driver")).newInstance();
return DriverManager.getConnection(cfg.getProperty("url"), cfg
.getProperty("user"), cfg.getProperty("password"));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("參數配置不正確 ");
}
}
public static void close(Connection con, Statement st, ResultSet rs) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
close(con, st);
}
public static void close(Connection con, Statement st) {
try {
st.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -