📄 databaseutil.java
字号:
/**
*
*/
package org.yqing.util;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.yqing.sso.Constant;
/**
* @author <a href="mailto:yangxiaonian@163.com">Cabby</a>
* Database handler
*
*/
public class DataBaseUtil {
private static final Log log = LogFactory.getLog(DataBaseUtil.class);
/**
* 通过数据源获取数据库连接
* @return
*/
public static Connection getConnection(){
Connection conn = null;
try{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/"+Constant.RESOURCE_NAME);
if(ds == null){
log.error("Can not get DataSource");
throw new Exception("Can not get DataSource");
}
conn = ds.getConnection();
}catch(Exception e){
e.printStackTrace();
log.error("SomeThing is Error where getConnection :"+e.getMessage());
}
return conn;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -