DBOperation:
package com.community.util;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DBOperation {
private PreparedStatement stsm;
private static Logger log = LoggerFactory.getLogger(DBOperation.class);
public void excuteUpdate(String sql,Object[]values,ServletContext sc)
{
try {
stsm = DBConnection.getConnection(sc).prepareStatement(sql);
if(values!=null && values.length>0)
{
for(int i=0 ; i<values.length ;i++)
{
stsm.setObject(i+1, values[i]);
}
}
stsm.executeUpdate();
} catch (SQLException e) {
log.error("数据更新错误!",e);
} finally {
DBConnection.closeConnection();
}
}
}
package com.community.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class DBConnection {
private static Connection conn;
private static Logger log = LoggerFactory.getLogger(DBConnection.class);
/**
* 获得数据库连接
*
* @return
*/
public static synchronized Connection getConnection(ServletContext sc) {
init(sc);
return conn;
}
/**
* 初始化数据库连接
*/
private static void init(ServletContext sc) {
try {
//PropUtils p = new PropUtils();
//p.loadFile("jdbc.properties");
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
//new FileSystemXmlApplicationContext();
ProxoolDataSource prox = (ProxoolDataSource)ctx.getBean("dataSource");
// 获取连接
conn =prox.getConnection();
// Class.forName(p.getValue("db.driver"));
// conn =
// DriverManager.getConnection(p.getValue("db.url"),p.getValue("db.user"),p.getValue("db.password"));
} catch (SQLException e) {
log.error("数据库连接错误!", e);
}catch(Exception e){
log.error("数据库连接错误!", e);
}
}
/**
* 关闭数据库连接
*/
public static void closeConnection() {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.error("数据库关闭错误!", e);
}
}
}
// public static void main(String[] args) {
// init();
// }
}
分享到:
相关推荐
这是servlet中ServletContext的应用实例,主要就是添加属性和读取属性。
ServletContext代码,开发工具 Myeclipse
ServletContext与ServletConfig关系
博客地址:...存放数据指的是这三个对象在工程中setAttribute(),removeAttribute()的时候,我们的监听器就会知道,然后在对应的方法中执行相应的操作。
对servletContext的学习,并使用实例进行了实战。完成了一个登录和统计登录人数的功能。
Ssh获取ServletContext 只需要更改struts-config action中的type就可以了ru :type="com.uo.spring.SpringDelegatingActionProxy" 大家有什么疑问可以直接去我的blog查看...《[正解]Ssh获取ServletContext》
ServletContext读取web应用中的资源文件
quartz servletcontext quartz servletcontext配置资料quartz servletcontext配置资料
超全面javaweb教程28天第9天_14_获取ServletContext对象
NULL 博文链接:https://onway417.iteye.com/blog/2194540
要把初始化参数认为是部署时常量,可用在运行时得到这些初始化参数,但不能设置初始化参数,根本没有setInitParameter().如果修改XML来改变一个初始化参数的值,只有当Web应用重新部署时Web应用其他部分才会看到,...
介绍 ServletContext对象的一些重要方法的示例代码
(5)ServletContext对象可以获取一些全局的配置信息,例如数据库的一些信息,可以配置xml文件当中 ,之后,通过ServletContext对象获取。这样配置的好处是每个servlet都可以直接访问,而不需要为每 个servlet进行...
Servlet生命周期,及servlet相关知识点的总结。
ServletContext官方叫servlet上下文。服务器会为每一个工程创建一个对象,这个对象就是ServletContext对象。这个对象全局唯一,而且工程内部的所有servlet都共享这个对象。所以叫全局应用程序共享对象。
Application对象(ServletContext对象).pptx
很清楚的讲解了ServletContext和ServletConfig,不光有理论上的讲解,还有例子。
本篇文章是对javax.servlet.Servlet,ServletContext接口进行了纤细的分析介绍,需要的朋友参考下
超全面javaweb教程28天第9天_13_ServletContext概述
超全面javaweb教程28天第9天_15_演示ServletContext