(1,''haha'',''hehe'',''234444'',''13577788999'',''阿斯地方''),
(2,''lei'',''aaa'',''450000'',''13939012107'',''郑州市经三路'');
三、Spring Bean容器环境获取工具类
package com.lavasoft.dbtest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by IntelliJ IDEA.
* File: ContextHelper.java
* User: leizhimin
* Date: 2008-2-21 14:20:46
* Spring Bean容器环境获取工具类
*/
public final class ContextHelper {
private static ClassPathXmlApplicationContext _ctx;
static {
_ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
}
private ContextHelper() {
}
public static ClassPathXmlApplicationContext getContext() {
return _ctx;
}
}
Spring数据源的灵活配置巧应用(3)
时间:2011-06-01
三、写获取数据库工具类
package com.lavasoft.dbtest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.CallableStatement;
/**
* Created by IntelliJ IDEA.
* File: DBUtil.java
* User: leizhimin
* Date: 2008-2-21 14:26:30
* 数据库工具
*/
public final class DBUtil {
private static final Log log = LogFactory.getLog(DBUtil.class);
/**
* 获取系统的数据源
*
* @return DataSource
*/
public static DataSource getDataSource() {
DataSource dataSource = null;
try {
dataSource = (DataSource) ContextHelper.getContext().getBean("rptds");
} catch (Exception e) {
log.error("获取数据源出错,请检查Spring数据源配置!");
}
return dataSource;
}
/**
* 获取数据库连接
*
* @return Connection
*/
public static Connection makeConnection() {
Connection conn = null;
try {
conn = getDataSource().getConnection();
} catch (SQLException e) {
log.error("通过数据源获取数据库连接发生异常!");
e.printStackTrace();
}
return conn;
}
/**
* 执行没有参数的SQL过程
*
* @param procedureName 存储过程名字
* @return boolean 返回存储过程执行的结果,true表示执行成功,false表示执行失败.
*/
public static boolean executeBSDProcedure(String procedureName) {
boolean flag = false;
String sqlStr = "{call " + procedureName + "()}";
CallableStatement cs;
Connection conn = makeConnection();
try {
cs = (CallableStatement) conn.prepareStatement(sqlStr);
cs.executeUpdate(sqlStr);
flag
|