1、导入相关jar包
2、修改web.xml
<?xml version="1.0" encoding="UTF-8"?>
ttp://java.sun.com/dtd/web-app_2_3.dtd">http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- Spring Configures -->
contextConfigLocation
/WEB-INF/configs/base-context.xml,
<!-- Dorado Filter -->
doradofilter
com.bstek.dorado.core.DoradoFilter
GZIPEncoder
com.bstek.dorado.core.GZIPEncodeFilter
GZIPEncoder
*.jsp
doradofilter
/*
<!-- Spring Loader -->
org.springframework.web.context.ContextLoaderListener
<!-- Dorado Serlvet -->
doradoservlet
com.bstek.dorado.core.DoradoServlet
2
doradoservlet
*.d
http://www.bstek.com/dorado
/WEB-INF/dorado.tld
3、在WEB-INF/configs下添加base-context.xml文件
<?xml version="1.0" encoding="UTF-8"?>
BR> "http://www.springframework.org/dtd/spring-beans.dtd">
<!-- DataSource -->
4、测试代码
package org.pan.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class TestJdbc extends TestCase {
private static ApplicationContext beanFactory ;
private DataSource dataSource ;
private Connection conn ;
private PreparedStatement pstmt ;
private ResultSet rs ;
protected void setUp() throws Exception {
try{
if(beanFactory==null)
beanFactory = new FileSystemXmlApplicationContext("/web/WEB-INF/configs/base-context.xml");
}catch(Exception e){
e.printStackTrace();
}
}
protected void tearDown() throws Exception {
try{
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void testConn(){
try{
dataSource = (DataSource)beanFactory.getBean("dataSource");
conn = dataSource.getConnection();
pstmt = conn.prepareStatement("select count(*) from article ");
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1));
}
}catch(Exception e){
e.printStackTrace();
Assert.fail(e.getLocalizedMessage());
}
}
}
一切OK
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10343122/viewspace-256930/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10343122/viewspace-256930/